Mr Andersson

Posts Tagged ‘winrm

How to get $VerbosePreference applied on remote commands

leave a comment »

If you use Write-Verbose in a script executed on a remote computer, you may not get the result you expect.

Example TestRemoteCommand.ps1:

Invoke-Command -ComputerName comp1 -ScriptBlock { 
    Write-Host "hello"
    Write-Verbose "there"
}

The above will only output “hello” and not “there” in the host window.

I thought I was clever by just passing along the $VerbosePreference value:

Invoke-Command -ComputerName comp1 -ArgumentList $VerbosePreference -ScriptBlock { 
    param( $VerbosePreference )
    Write-Host "hello"
    Write-Verbose "there"
}

But, still no luck! Only the statement using Write-Host is included in the output when the command is invoked on the remote computer.
What’s going on here is that the argument sent to the remotely executed script block is converted to an integer, which is the base integral type of the underlying enum type of $VerbosePreference.
So I figured that typing the argument in the param() clause will bind the parameter to the correct data type, and voila, it now works as expected.

Invoke-Command -ComputerName comp1 -ArgumentList $VerbosePreference -ScriptBlock { 
    param( [System.Management.Automation.ActionPreference] $VerbosePreference )
    Write-Host "hello"
    Write-Verbose "there"
}

Now you can toggle the verbose output of your remote script using the switch of your local script:

.\TestRemoteCommand.ps1 # Default, or -Verbose:$false
.\TestRemoteCommand.ps1 -Verbose

Written by anderssonjohan

November 19, 2009 at 14:25

Posted in scripting

Tagged with ,

Enable-PSRemoting is broken in PowerShell?

with 2 comments

Yesterday I installed the RTM bits of WinRM 2.0 on a Windows Server 2003 machine and it were really no issues at all getting it working.

Today I tried to do the same thing but on a Windows 2008 Server (R1) that is not joined to any domain, and it fail on step 1 of the configuration.
In both cases I started out downloading the patch. Then I run “Enable-PSRemoting” in an elevated PowerShell prompt, which succeeded on the Windows 2003 Server but not on the Windows 2008 Server.
The error I got was “Access denied”.
I thought I followed the troubleshooting guide for WinRM, but apparently there is slight difference of two different ways I found on how you set this up. You can either run “Enable-PSRemoting” or you can run “winrm qc”. If I run the quick config tool using the “winrm” command everything works out just fine, but if I use “Enable-PSRemoting” I get “Access denied”.
The most mysterious part of this is that “Enable-PSRemoting” WORKED on Windows Server 2003, but not on Windows Server 2008. Strange!
If you have any clues about this, please leave me a comment!

Written by anderssonjohan

November 18, 2009 at 07:57

Posted in scripting, troubleshooting

Tagged with ,