繁体   English   中英

Powershell脚本有效,但在vb.net代码中无效

[英]Powershell script works but not in vb.net code

我需要从vb.net运行Powershell脚本。 我编写了该文件,并在Powershell ISE和Powershell控制台中对其进行了尝试,并且它工作正常。

但是,当我尝试在vb.net代码中使用它时,什么也没发生。

该脚本由某些用户放入文本文件citrix会话中。

这是我的powershell脚本:

$username = 'domain\myusername'
$password = 'mypassword'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Credential $credential -NoNewWindow -ArgumentList "add-pssnapin citrix*`nGet-BrokerSession -AdminAddress 'ipadresshere' | Out-File -FilePath C:\inetpub\wwwroot\gestusers\ajax\data\arrays.txt"

这是我的vb.net代码:

Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()

        ' open it
        MyRunSpace.Open()

        ' create a pipeline and feed it the script text
        Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()

        MyPipeline.Commands.AddScript(scriptText)

        ' add an extra command to transform the script output objects into nicely formatted strings
        ' remove this line to get the actual objects that the script returns. For example, the script
        ' "Get-Process" returns a collection of System.Diagnostics.Process instances.
        MyPipeline.Commands.Add("Out-String")

        ' execute the script
        Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

        ' close the runspace
        MyRunSpace.Close()

        ' convert the script result into a single string
        Dim MyStringBuilder As New StringBuilder()

        For Each obj As PSObject In results
            MyStringBuilder.AppendLine(obj.ToString())
        Next

        ' return the results of the script that has
        ' now been converted to text
        Return MyStringBuilder.ToString()

当我尝试使用较小的脚本时:

$test = "blablabla"
$test | Out-File -filepath "C:\inetpub\wwwroot\gestusers\ajax\data\arrays.txt"

它有效,所以我不太了解为什么第一个脚本不起作用..如果您有任何建议,我将非常感激

我终于找到了解决我问题的方法。 我在IIS服务器上运行我的Web应用程序,并且在启动脚本时,它是默认用户“ IIS default / application_pool”,尽管我指定了特定用户来运行它,但它正在启动它,就像我的脚本没有工作。

因此,我选择进入IIS服务器参数,并与特定用户建立了另一个app_pool。 我将我的应用程序放在此池下,现在可以正常使用了。

这是在IIS服务器上建立特定池的方法: https : //www.developer.com/net/asp/article.php/2245511/IIS-and-ASPNET-The-Application-Pool.htm

ya

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM