AjarnJonesy
asked on
Powershell Script help
Hi All
I need some guidance with this script I have been working on for some time. Basically what the script does in its simplest terms is look at a list of servers and either start/stop services based on the arguments in the array. I am trying to make this a bit more scalable by pulling in server/service/Some_other_ script. then once the operation is complete write out to a log file.
From my original script I have added a line.split with 3 current variables using a comma as the delimiter. Im just having problems figuring out how to correctly pass these values in to the array and make it work. The code below is for my service restart script I have been working on. The original script uses the write-host cmdlet to post everything to the console. I am trying to remove that for obvious reasons.
Input file format is
Server_Name,W3SVC,SomePS.p s1
The main script is as follows.
# Setup trap to catch exceptions
trap [Exception]
{
write-error $("TRAPPED: " + $_.Exception.Message);
}
$computers = Get-Content C:\scripts\servers.txt;
$computer = $line.split(',')[0]
$ServiceName = $line.split(',')[1]
$Operation = $line.split(',')[2]
# Setup the Service array with the service names we want to check are running
$serviceArray = '$ServiceName';
# Powershell Array
foreach($computer in $computers)
{
#Write-Host "Checking $computer";
$objWMIService = Get-WmiObject -Class win32_service -computer $computer
foreach($service in $objWMIService)
{
# Check each service in $serviceArray
foreach($srv in $computers)
{
if($service.name -eq $srv)
{
if($service.state -eq "running")
{
}
else
{
if($start -eq $true)
{
$serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");
$name = $serviceInstance.Name;
$serviceInstance.StartServ ice() | Out-Null;
# Refresh the object instance
$serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");
$state = $serviceInstance.State;
Write-Host "$name is ""$state"" on $computer." >>C:\scripts\log.txt;
}
}
}
}
}
}
I need some guidance with this script I have been working on for some time. Basically what the script does in its simplest terms is look at a list of servers and either start/stop services based on the arguments in the array. I am trying to make this a bit more scalable by pulling in server/service/Some_other_
From my original script I have added a line.split with 3 current variables using a comma as the delimiter. Im just having problems figuring out how to correctly pass these values in to the array and make it work. The code below is for my service restart script I have been working on. The original script uses the write-host cmdlet to post everything to the console. I am trying to remove that for obvious reasons.
Input file format is
Server_Name,W3SVC,SomePS.p
The main script is as follows.
# Setup trap to catch exceptions
trap [Exception]
{
write-error $("TRAPPED: " + $_.Exception.Message);
}
$computers = Get-Content C:\scripts\servers.txt;
$computer = $line.split(',')[0]
$ServiceName = $line.split(',')[1]
$Operation = $line.split(',')[2]
# Setup the Service array with the service names we want to check are running
$serviceArray = '$ServiceName';
# Powershell Array
foreach($computer in $computers)
{
#Write-Host "Checking $computer";
$objWMIService = Get-WmiObject -Class win32_service -computer $computer
foreach($service in $objWMIService)
{
# Check each service in $serviceArray
foreach($srv in $computers)
{
if($service.name -eq $srv)
{
if($service.state -eq "running")
{
}
else
{
if($start -eq $true)
{
$serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");
$name = $serviceInstance.Name;
$serviceInstance.StartServ
# Refresh the object instance
$serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");
$state = $serviceInstance.State;
Write-Host "$name is ""$state"" on $computer." >>C:\scripts\log.txt;
}
}
}
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm not sure why I have so much trouble getting my head around Powershell.. But any way..
Thanks for the responses . Basically with the third entry in the servers.txt Operation I want to basically be able to use that to call another Powershell Script. In this case, if I have a server then service listed. Lets say starting W3SVC the I want to call a powershell script that will invoke-webrequest to test the site.
So its basically Look at the server name, check the service w3svc and start then call a separate powershell script that will do something simple like PS C:\> Invoke-WebRequest http://mysite.com.... So essentially all I am trying to do with operation is call another powershell script... I'm trying to make this as scalable as possible where if I need to call different scripts with Operation (depending on the server and its requirements) I can go that route. not sure that's the best way to do it but it is/has been a learning experience.
Thanks
Thanks for the responses . Basically with the third entry in the servers.txt Operation I want to basically be able to use that to call another Powershell Script. In this case, if I have a server then service listed. Lets say starting W3SVC the I want to call a powershell script that will invoke-webrequest to test the site.
So its basically Look at the server name, check the service w3svc and start then call a separate powershell script that will do something simple like PS C:\> Invoke-WebRequest http://mysite.com.... So essentially all I am trying to do with operation is call another powershell script... I'm trying to make this as scalable as possible where if I need to call different scripts with Operation (depending on the server and its requirements) I can go that route. not sure that's the best way to do it but it is/has been a learning experience.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$line is never set up, but that doesn't matter as you aren't using the resulting vars anyway ...
The usual way to read such a delimited file is by - import-csv! In your case (no column headers, comma-separated):
Open in new window