来自ASP.net应用程序的PowerShell Invoke-Command

时间:2015-01-15 17:21:04

标签: c# powershell winrm

我在c#中有一个asp .net webservice,它通过invoke-command工具(winrm)在远程主机中执行powershell脚本。 如果我直接从powershell执行invoke-command,这样可以正常工作。

Invoke-Command -ConnectionURI http://localhost:5985 -ScriptBlock {new-item c:\\dir1 -itemtype directory} -Credential $mycreds

其中$ mycreds设置为用户名和密码

但是当我尝试从c#

执行相同的操作时
String cmd = "$secpasswd = ConvertTo-SecureString 'password' -AsPlainText -force; 
              $mycreds = New-Object System.Management.Automation.PSCredential ('Administrador', $secpasswd);
              Invoke-Command -ConnectionURI http://localhost:5985 -ScriptBlock {new-item c:\\dir1 -itemtype directory} -Credential $mycreds";

 Runspace runspace = RunspaceFactory.CreateRunspace();
 runspace.Open();
 Pipeline pipeline = runspace.CreatePipeline();
 pipeline.Commands.AddScript(cmd);
 Collection<PSObject> results = pipeline.Invoke();
 return (results.Count).ToString();

计数返回0,但是如果我更改了脚本:

   String cmd = "$secpasswd = ConvertTo-SecureString 'password' -AsPlainText -force; 
echo $secpasswd";

计数返回1,并打印:“System.Security.SecureString”。然后我确信c#脚本没关系,但我在PowerShell中有一些权限问题。

我的winrm配置是:

> PS C:\Users\Administrador> winrm get winrm/config Config
>     MaxEnvelopeSizekb = 150
>     MaxTimeoutms = 60000
>     MaxBatchItems = 32000
>     MaxProviderRequests = 4294967295
>     Client
>         NetworkDelayms = 5000
>         URLPrefix = wsman
>         AllowUnencrypted = false
>         Auth
>             Basic = true
>             Digest = true
>             Kerberos = true
>             Negotiate = true
>             Certificate = true
>             CredSSP = false
>         DefaultPorts
>             HTTP = 5985
>             HTTPS = 5986
>         TrustedHosts = *
>     Service
>         RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)S:P(AU;FA;GA;;;WD)(AU;SA;GWGX;;;WD)
>         MaxConcurrentOperations = 4294967295
>         MaxConcurrentOperationsPerUser = 15
>         EnumerationTimeoutms = 60000
>         MaxConnections = 25
>         MaxPacketRetrievalTimeSeconds = 120
>         AllowUnencrypted = false
>         Auth
>             Basic = false
>             Kerberos = true
>             Negotiate = true
>             Certificate = false
>             CredSSP = false
>             CbtHardeningLevel = Relaxed
>         DefaultPorts
>             HTTP = 5985
>             HTTPS = 5986
>         IPv4Filter = *
>         IPv6Filter = *
>         EnableCompatibilityHttpListener = false
>         EnableCompatibilityHttpsListener = false
>         CertificateThumbprint
>     Winrs
>         AllowRemoteShellAccess = true
>         IdleTimeout = 180000
>         MaxConcurrentUsers = 5
>         MaxShellRunTime = 2147483647
>         MaxProcessesPerShell = 15
>         MaxMemoryPerShellMB = 150
>         MaxShellsPerUser = 5

我也跑:

Enable-PsRemoting -Force

我设置

Set-ExecutionPolicy Unrestricted

在我的Session_Configuration中,我得到:

PS C:\Users\Administrador>  Get-PSSessionConfiguration | Format-List -Property Name, Permission


Name       : microsoft.powershell
Permission : BUILTIN\Administradores AccessAllowed

Name       : Microsoft.PowerShell32
Permission :

Name       : microsoft.ServerManager
Permission : BUILTIN\Administradores AccessAllowed

我想,microsoft.ServerManager是IIS。

我的winrm版本:

PS C:\Users\Administrador> winrm id
IdentifyResponse
    ProtocolVersion = http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
    ProductVendor = Microsoft Corporation
    ProductVersion = OS: 6.1.7601 SP: 1.0 Stack: 2.0

和PowerShell版本:

PS C:\ Users \ Administrador&gt;得到 - 主机

名称:ConsoleHost 版本:2.0 InstanceId:5eb97936-a0a4-450d-b2cc-57069d1ea7e6 UI:System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture:es-CO CurrentUICulture:es-ES PrivateData:Microsoft.PowerShell.ConsoleHost + ConsoleColorProxy IsRunspacePushed:错误 Runspace:System.Management.Automation.Runspaces.LocalRunspace

1 个答案:

答案 0 :(得分:0)

我转到任务管理器,进程选项卡并查看w3wp.exe,正在执行该进程的用户是#34; DefaultAppPool&#34;,我猜这个用户没有权限在PowerShell上远程访问或执行。 (图像显示Administrador,因为我已经更改了它) enter image description here

要更改用户,请转到IIS管理器,在应用程序组中,我的站点的应用程序组显示在Identity:ApplicationPoolIdentity中,选择应用程序池并单击高级设置,在Process Model中,选择Identity并单击省略号(带有三个点的按钮)。在个人帐户中设置具有用户名和密码的管理员帐户,重新启动IIS。

enter image description here

如果我按任务管理器查看,则iis用户已更改