Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

power shell script in taskschedular is not giving output

$
0
0

Hi

I am running below script in task schedular through taskschedular.ps1 script given below:

===================
Taskschedular.ps1 script

Import-Csv "D:\script\serverlist.csv" | foreach {
$server = $_.rebootservername
$taskschedularservername = $_.taskservername
$reboottime = $_.RebootTime
$rebootdate = $_.RebootDate
$TaskSchedulerunnigtime = $_.schedule
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$password = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your 'Domain password' to create schedular task")
schtasks.exe /create /S $taskschedularservername /RU "contoso\admin" /RP $password /sc $TaskSchedulerunnigtime /st $reboottime /sd $rebootdate /tn rebootserver_$server /tr "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe D:\script\reboot.ps1"}


======================================

that taskschedular script create task correct and start on its time.

But below script runs perfectly while I run on powershell console manually and give me output of Rebootlogs.txt and restart_servers.csv"

But when below script execute in task schedular, this script don't provide me "restart_servers.csv" file in output. I get only Rebootlogs.txt file as output.

Please help me or guide me.

===========================================

# Get list of Servers
$Servers = Import-CSV "D:\script\serverlist.csv" | Select-Object -ExpandProperty rebootservername
$file = "D:\script\serverlist.csv"
$filter="*[System[EventID=1074] and EventData[Data='restart']]"

# Reboot each server
foreach ($Server in $Servers)
{
$logs = "D:\script\Rebootlogs.txt"
"Computer $Server initiated reboot at $(Get-Date)" | Add-Content -Path $logs
Restart-Computer $Server -Force
}

# Give them a minute to start
Start-Sleep -Seconds 60

# Check each Server
foreach ($Server in $Servers)
{
if (Test-Connection $Server -quiet) { "Computer $Server verified to be responding to ping at $(Get-Date)" | Add-Content -Path $logs }
else { "Computer $Server unresponsive to ping at $(Get-Date)" | Add-Content -Path $logs }

foreach ($server in $servers)
{
$Servers = Import-CSV "D:\script\serverlist.csv" | Select-Object -ExpandProperty rebootservername
$outfile = "D:\script\restart_servers.csv"
$filter="*[System[EventID=1074] and EventData[Data='restart']]"
      Get-WinEvent -LogName System -ComputerName $servers -MaxEvents 1 -FilterXPath $filter | select MachineName,EventID,TimeCreated,@{N='Type';E={$_.properties[4].Value}} |
Export-Csv Event_$Servers.CSV -Notypeinformation}
}

=======================================

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>