如何在 ASP.NET 站点中引用 PowerShell 模块
我试图弄清楚如何在 ASP.NET 网站(使用 vb.NET)中使用 Microsoft Online Services 迁移工具包 PowerShell 命令。
我开始使用有关如何在 ASP.NET 中使用 PowerShell 的指南 - 从这里开始: http://devinfra-us.blogspot.com/2011/02/using-powershell-20-from-aspnet-part-1.html
我正在尝试找出如何实施在线服务迁移工具包 PowerShell cmdlet。
这是我的代码隐藏片段:
Sub GetUsers()
Dim iss As InitialSessionState = InitialSessionState.CreateDefault()
iss.ImportPSModule(New String() {"MSOnline"})
Using myRunSpace As Runspace = RunspaceFactory.CreateRunspace(iss)
myRunSpace.Open()
' Execute the Get-CsTrustedApplication cmdlet.
Using powershell As System.Management.Automation.PowerShell = System.Management.Automation.PowerShell.Create()
powershell.Runspace = myRunSpace
Dim connect As New Command("Get-MSOnlineUser -Enabled")
Dim secureString As New System.Security.SecureString()
Dim myPassword As String = "ThePassword"
For Each c As Char In myPassword
secureString.AppendChar(c)
Next
connect.Parameters.Add("Credential", New PSCredential("[email protected]", secureString))
powershell.Commands.AddCommand(connect)
Dim results As Collection(Of PSObject) = Nothing
Dim errors As Collection(Of ErrorRecord) = Nothing
results = powershell.Invoke()
errors = powershell.Streams.[Error].ReadAll()
For Each obj As PSObject In results
Response.Write(obj.Properties("Identity").Value.ToString())
Next
End Using
End Using
End Sub
当我尝试通过页面运行代码时,出现以下错误
术语“Get-MSOnlineUser -Enabled”未被识别为以下名称 cmdlet、函数、脚本文件或可操作程序。检查 名称的拼写,或者如果包含路径,请验证该路径 正确并重试。
所以我猜我还没有弄清楚如何导入在线服务迁移工具包 PowerShell CmdLets。我也不确定这行
iss.ImportPSModule(New String() {"MSOnline"})
是否完全正确。有没有办法可以验证模块名称?
我也不确定在哪里以及如何引用 .dll 文件。目前我已将它们复制到我的 bin 文件夹中,但无法将它们添加为引用,那么 ImportPSModule 语句如何知道在哪里可以找到它们?特别是当网站发布到最终生产服务器时。
另一个问题是,我应该使用 x86 还是 x64 cmdlet?我正在 Win7 x64 上进行开发,但不确定网站是构建为 x86 还是 x64?我需要了解服务器是什么架构吗?
I'm trying to figure out how to use the Microsoft Online Services Migration Toolkit PowerShell Commands from within an ASP.NET website (using vb.NET).
I've started off using a guide on how to use PowerShell in ASP.NET - from here: http://devinfra-us.blogspot.com/2011/02/using-powershell-20-from-aspnet-part-1.html
I'm trying to work out how to implement the Online Services Migration Toolkit PowerShell cmdlets.
Here is a snippet from my code-behind:
Sub GetUsers()
Dim iss As InitialSessionState = InitialSessionState.CreateDefault()
iss.ImportPSModule(New String() {"MSOnline"})
Using myRunSpace As Runspace = RunspaceFactory.CreateRunspace(iss)
myRunSpace.Open()
' Execute the Get-CsTrustedApplication cmdlet.
Using powershell As System.Management.Automation.PowerShell = System.Management.Automation.PowerShell.Create()
powershell.Runspace = myRunSpace
Dim connect As New Command("Get-MSOnlineUser -Enabled")
Dim secureString As New System.Security.SecureString()
Dim myPassword As String = "ThePassword"
For Each c As Char In myPassword
secureString.AppendChar(c)
Next
connect.Parameters.Add("Credential", New PSCredential("[email protected]", secureString))
powershell.Commands.AddCommand(connect)
Dim results As Collection(Of PSObject) = Nothing
Dim errors As Collection(Of ErrorRecord) = Nothing
results = powershell.Invoke()
errors = powershell.Streams.[Error].ReadAll()
For Each obj As PSObject In results
Response.Write(obj.Properties("Identity").Value.ToString())
Next
End Using
End Using
End Sub
When I try to run the code via the page, I'm getting the following error
The term 'Get-MSOnlineUser -Enabled' is not recognized as the name of
a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path
is correct and try again.
So I'm guessing I haven't worked out how to import the Online Services Migration Toolkit PowerShell CmdLets. I'm also not exactly sure if the line:
iss.ImportPSModule(New String() {"MSOnline"})
Is exactly correct. Is there a way I can verify the Module name?
I'm also unsure of where and how to reference the .dll files. At the moment I have copied them to my bin folder but I can't add them as references, so how does the ImportPSModule statement know where to find them? Especially when the website is published to the final production server.
One other question, should I be using the x86 or x64 cmdlets? I'm developing on Win7 x64, but not sure if the website builds as x86 or x64? Do I need to find out what architecture the server is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“Get-MSOnlineUser -Enabled”不是命令; “Get-MSOnlineUser”是。我有点困惑你如何使用
connect.Parameters.Add("Credential", ...)
在脚本中进一步正确地得到它,但没有对 -Enabled 做同样的事情。使用 connect.AddArgument("Enabled") 或 connect.Parameters.Add("Enabled", true) 就可以了。
"Get-MSOnlineUser -Enabled" is not a command; "Get-MSOnlineUser" is. I'm a bit confused how you got it correct further down the script with
connect.Parameters.Add("Credential", ...)
but didn't do the same thing for -Enabled.Use
connect.AddArgument("Enabled")
orconnect.Parameters.Add("Enabled", true)
and you should be good to go.