Lately, I had to execute Powershell very simple script on a large list of Windows systems, but lot of them weren’t ready for it, so I ended up having to do some hacking, and previously checking if the system was ready to accept external commands.

The most tricky thing was to check for availability of WinRM remotely, then being able to enable when necessary prior executing Invoke-Command from Powershell.

To check WinRM remotely:

$WinRMEnabled = [bool](Test-WSMan -ComputerName $srv -ErrorAction SilentlyContinue)

After including this sentence, and because was not possible using Powershell to enable WinRM, we had to use psexec this way:

If (!($WSManEnabled)) {
    .\PsExec.exe -h -u "user" -p "passwd" \\$server C:\Windows\System32\winrm.cmd quickconfig -quiet

Then, after WinRM enabled, I had to unzip certain file over Powershell 2.0, not an easy task, but finally, I found out this very nice solution:

Invoke-Command -ComputerName $srv -Credential $mycred -ScriptBlock {
   Function Expand-ZIPFile ($fileZIP, $unzipPATH) {
      $shell = new-object -com shell.application
      $zip = $shell.NameSpace($fileZIP)
      foreach($item in $zip.items()) {
   Expand-ZipFile "C:\SoftwareBase\TCPdata\" -unzipPATH "C:\SoftwareBase\TCPdata"

Goal achieved, WinRM full operative and file unzipped in the end!


Leave a Reply

%d bloggers like this: