远程桌面PowerShell脚本

时间:2012-04-17 21:30:30

标签: .net powershell remote-desktop

我无法使用.net访问UserAssignment容器。我能够进入应用程序容器,但没有进一步。任何想法将不胜感激。我正在使用的代码是:

Private Function runScript2(userName As String, applicationName As String) As String
    Dim rs As Runspace = RunspaceFactory.CreateRunspace
    rs.Open()

    Dim ps As PowerShell = PowerShell.Create
    ps.Runspace = rs
    Dim sb2 As New StringBuilder
    sb2.AppendLine("Import-Module -Name C:\RemoteDesktopServices\RemoteDesktopServices.psd1")
    sb2.AppendLine("Get-ChildItem -Path RDS:\") 'this works
    sb2.AppendLine("Get-ChildItem -Path RDS:\RemoteApp\RemoteAppPrograms\WordPad\")  'this works
    sb2.AppendLine("Get-ChildItem -Path RDS:\RemoteApp\RemoteAppPrograms\WordPad\UserAssignment") 'this does not work, no error
    sb2.AppendLine("Get-ChildItem -Path C:\MyScripts")
    ps.AddScript(sb2.ToString)

    Dim output As Collection(Of PSObject) = ps.Invoke
    Dim sb As New StringBuilder
    For Each obj As PSObject In output
        If TypeOf (obj.BaseObject) Is DirectoryInfo Then
            Dim fle As DirectoryInfo = CType(obj.BaseObject, DirectoryInfo)
            sb.AppendLine(fle.FullName)
        ElseIf TypeOf (obj.BaseObject) Is FileInfo Then
            Dim fle As FileInfo = CType(obj.BaseObject, FileInfo)
            sb.AppendLine(fle.FullName)
        Else
            sb.AppendLine(obj.BaseObject.ToString)
        End If
    Next
    Return sb.ToString

End Function

1 个答案:

答案 0 :(得分:0)

它没有失败,你根本没有任何UserAssignment,所以它什么也没有给你回报。只要你添加一些用户 - 它就会返回它。您可以通过Computer Management控制台

手动添加用户来检查它