Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 Powershell相关问题_C# 4.0_Powershell_Powershell 3.0 - Fatal编程技术网

C# 4.0 Powershell相关问题

C# 4.0 Powershell相关问题,c#-4.0,powershell,powershell-3.0,C# 4.0,Powershell,Powershell 3.0,以下是我的情况: 1) 我已经使用“RunspaceConfiguration”——C代码——来调用powershell脚本 2) 脚本中的内容也很简单——使用regsvr32注册COM dll Invoke-Command -ScriptBlock { & 'regsvr32.exe' $registerdllfullpath } 3) 如果没有C#代码,为了调用regsvr32,我们必须右键单击并选择“以管理员身份运行”来启动powershell 那我该怎么做呢 有人建议您使用以

以下是我的情况:

1) 我已经使用“RunspaceConfiguration”——C代码——来调用powershell脚本

2) 脚本中的内容也很简单——使用regsvr32注册COM dll

Invoke-Command -ScriptBlock { & 'regsvr32.exe' $registerdllfullpath }
3) 如果没有C#代码,为了调用regsvr32,我们必须右键单击并选择“以管理员身份运行”来启动powershell

那我该怎么做呢


有人建议您使用以下方法:

$user = Get-Credential 
Invoke-Command -ComputerName $servername -Credential $user -ScriptBlock { & 'regsvr32.exe' $registerdllfullpath }  
但是:

1) -如果没有-ComputerName,则无法使用凭据。但如果只是本地机器,我们仍然需要打开Windows远程管理服务,这有点笨拙

2) 因为我已经是触发powershell的本地管理员,所以它会再次请求我的用户名和密码,这似乎是多余的

3) 最后但并非最不重要的是,它不起作用…:-(

非常感谢

---------------------------编辑-----------------

我在谷歌上搜索了一个解决方案,在我的PS脚本中是这样的:[它完成了任务]

Start-Process powershell -Verb runAs -ArgumentList "Invoke-Command -ScriptBlock { & ","'regsvr32.exe'", "$registerdllfullpath } "

原来的脚本现在变成了参数列表,我不确定这是不是正确的方法:-)

使用
runas来本地提升UAC是正确的方法

但是,请记住,UAC仅适用于本地。如果您有一个非提升的管理员帐户,该帐户也是远程计算机上的管理员,则直接远程脚本将使用提升远程运行(不使用
runas
或类似工具)

假设您要远程管理时启用远程管理(使用Windows RMS)