Link to home
Start Free TrialLog in
Avatar of Alex
AlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Sanity check on my powershell script please

OK,

So this PowerShell script is meant to do the following

  1. Pull the list of computers
  2. Connect to the machine and check if it's C$ or support$
  3. Copy the TillSC folder which contains PSexec etc
  4. Execute the .com file which runs the SC command to delete a service
  5. Report it back if it's not online.

$Computers = Get-content "C:\Powershell Projects\TillSC\computers.txt"
$source = "C:\Powershell Projects\TillSC"

foreach ($computer in $computers) {
    $X = gwmi win32_share -ComputerName $computer | Where-Object {$_.Name -eq "Support$"}
        if($X.Name -eq 'Support$')
        {
            $dest = "Support$\Temp"
        }
        else
        {
           $dest = "C$\Temp"
        }

    Copy-Item $source -Destination \\$computer\$dest -Recurse -Force
   } 


   
foreach ($computer in $computers) {
if (test-Connection -Cn $computer -quiet) {
C:\Powershell Projects\Tills\PSexec.exe -d -s "\\$computer"  "C:\Temp\TillSC\SCDelete.com"
} else {
"$computer is not online" | Out-file -Append "C:\Powershell Projects\TillSCcomputersnotonline.txt"
}
}

Open in new window


Does that look right? Before you ask, yes I've tested it but I see no issue getting someone else to check it over :-)

Thanks

Alex
Avatar of oBdA
oBdA

Okay, you want a sanity check.
Then I have a question: why are you using two external programs (one of them being a .com, which AV software rightfully frowns upon these days) for something that sc.exe supports natively? sc.exe can remove services remotely as well.
$ServiceName = 'Some Service'
$Computers = Get-Content "C:\Powershell Projects\TillSC\computers.txt"
ForEach ($Computer in $Computers) {
	If (Test-Connection -ComputerName $Computer -Quiet) {
		& sc.exe "\\$($Computer)" delete $ServiceName
	} Else {
		"$($Computer) is not online" | Out-File -Append "C:\Powershell Projects\TillSC\computersnotonline.txt"
	}
}

Open in new window

Avatar of Alex

ASKER

Because I can't execute the SC command through PSexec for some reason.

Plus, well that's it, Btw, the .com file contains the following

SC delete WinVNC4, so technically I'm using SC I just put it in a .com batch file :(
You cannot have checked your script, because the psexec call uses an unquoted path containing spaces ...
SC can run directly against remote. No psexec required!
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

Oh, cool :-)

Thanks for that, I'll give it a shot :-)