{"id":764,"date":"2010-04-23T12:25:04","date_gmt":"2010-04-23T12:25:04","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?p=764"},"modified":"2022-10-31T03:01:37","modified_gmt":"2022-10-31T03:01:37","slug":"running-remote-powershell-scripts-from-vb-net","status":"publish","type":"post","link":"https:\/\/www.wapshere.com\/missmiis\/running-remote-powershell-scripts-from-vb-net","title":{"rendered":"Running Remote Powershell scripts from VB.NET"},"content":{"rendered":"<p>I&#8217;ve been writing an XMA that uses powershell to communicate with MSOnline. Because of various connectivity issues I have to run the powershell commands on a server other than the FIM server &#8211; that is, I have to use remote powershell. There will be more detail on this XMA in a future post, however today I&#8217;m going to write up just the VB.Net to remote powershell bit, because as usual I had to spend a lot of time going through blog posts and forums to supplement the meager information available on MSDN.<!--more--><\/p>\n<h3>Enabling Remote Powershell<\/h3>\n<p>I am running the code on a Windows 2008 server, and the powershell scripts on a Windows 2003 server. As neither of these OS&#8217;s natively include WinRM and powershell 2.0 you have to <a href=\"http:\/\/support.microsoft.com\/kb\/968929\">download <\/a>and install the Windows Management Framework update.<\/p>\n<p>Next you have to enable remoting and trust the remote host. Here is a good <a href=\"http:\/\/www.computerperformance.co.uk\/powershell\/powershell_remote.htm\">walkthrough<\/a>.<\/p>\n<h3>Libraries<\/h3>\n<p>You need the System.Management.Automation library, which you can copy out of the GAC and reference in your code:<\/p>\n<p><code>copy C:\\windows\\assembly\\GAC_MSIL\\System.Management.Automation\\1.0.0.0__31bf3856ad364e35\\System.Management.Automation.dll C:\\path_to_project_folder<\/code><\/p>\n<p>Then you&#8217;ll need these imports:<\/p>\n<p><code>Imports System.Management.Automation<br \/>\nImports System.Management.Automation.Host<br \/>\nImports System.Management.Automation.Runspaces<br \/>\n<\/code><\/p>\n<h3>The Code<\/h3>\n<p>I had a number of powershell commands to run on the remote server (including adding a snapin) so the simplest thing was to wrap it in a powershell script and just call that.<\/p>\n<p>Note also that I also had a problem with the script arguments (neither AddArgument nor AddParameter actually passed the arguments to the remote server), so when I call this subroutine I pass the script arguments along with the script name like so:<\/p>\n<p><code>RunRemotePSScript(\"myserver\", \"username\", \"password\", \"c:\\scripts\\myscript.ps1 \" &amp; arg1 &amp; \" \" &amp; arg2)<br \/>\n<\/code><\/p>\n<p>And here&#8217;s the code&#8230;<\/p>\n<div><\/div>\n<p><code><\/code><\/p>\n<p><code><code><\/code><\/code><\/p>\n<pre>    Sub RunRemotePSScript(ByVal connectTo As String, ByVal user As String, ByVal password As String, ByVal script As String)\n        Const SHELL_URI As String = \"http:\/\/schemas.microsoft.com\/powershell\/Microsoft.PowerShell\"\n        Dim serverUri As New Uri(\"http:\/\/\" &amp; connectTo &amp; \":5985\/wsman\")\n\n        Dim securepass As New Security.SecureString\n        Dim c As Char\n        For Each c In password\n            securepass.AppendChar(c)\n        Next\n        Dim remotecred As New PSCredential(user, securepass)\n\n        Dim connectionInfo As New WSManConnectionInfo(serverUri, SHELL_URI, remotecred)\n        Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace(connectionInfo)\n        Dim psresult As New System.Collections.ObjectModel.Collection(Of PSObject)\n        myRunSpace.Open()\n\n        Dim psh As PowerShell = PowerShell.Create()\n        psh.Runspace = myRunSpace\n\n        psh.AddScript(script)\n        psresult = psh.Invoke()\n        psh.Dispose()\n        myRunSpace.Close()\n    End Sub<\/pre>\n<p><code><code><\/code><\/code><\/p>\n<p><code><\/code><\/p>\n<h3>The biggest bruise from knocking my head<\/h3>\n<p>came from trying to get those two URI addresses correct. During that time I saw this error a lot:<\/p>\n<blockquote><p>&#8220;System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message :<br \/>\n&lt;h1&gt;Bad Request (Invalid Hostname)&lt;\/h1&gt;<br \/>\nFor more information, see the about_Remote_Troubleshooting Help topic.<\/p><\/blockquote>\n<p>Eventually this blog post got me on the right track: <a href=\"http:\/\/scorpiotek.com\/blog\/?p=770\">http:\/\/scorpiotek.com\/blog\/?p=770<\/a>\u00a0&#8211; so thanks for that!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been writing an XMA that uses powershell to communicate with MSOnline. Because of various connectivity issues I have to run the powershell commands on a server other than the FIM server &#8211; that is, I have to use remote powershell. There will be more detail on this XMA in a future post, however today&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[23,30],"tags":[],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-powershell","category-vbnet"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pkp1o-ck","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=764"}],"version-history":[{"count":7,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":3310,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/764\/revisions\/3310"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}