我正在创建停止IIS默认网站的应用程序.我使用PowerShell脚本来停止网站,因为该脚本是从我的网站执行的.
这是我的脚本:
Import-Module C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration
Stop-Website 'Default Web Site'
my copy code
Start-Website 'Default Web Site'
Run Code Online (Sandbox Code Playgroud)
这是我的C#代码:
PowerShell _PowerShell = PowerShell.Create();
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
_PowerShell.Runspace = rs;
_PowerShell.AddScript(@"Set-ExecutionPolicy RemoteSigned -scope LocalMachine").Invoke();
_PowerShell.AddScript(@"E:\DE.TEST\Power_Shell\Scripts\StopIISDefaultSite.ps1").Invoke();
if (_PowerShell.HadErrors)
{
Collection<ErrorRecord> errors = _PowerShell.Streams.Error.ReadAll();
foreach (var item in errors)
{
Console.WriteLine(item.ToString());
}
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
它显示以下错误
检索具有CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6}的组件的COM类工厂由于以下错误而失败:80040154未注册类(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG)).
我们有几个脚本用于安装和配置支持我们维护的系统的依赖项.我们在建立开发,测试,演示,培训,产品等环境时随时运行这些.我们经常发现我们必须处理x64与x86架构,特别是涉及powershell脚本的地方.
例如,我有一个脚本,它使用Windows Installer PowerShell Extensions来确定是否已安装程序/补丁.如果没有显式调用PowerShell(x86),该脚本在x64环境中不起作用,默认情况下,该脚本不在路径中.当我们将这些脚本移植到x64平台时,维护一组在两个体系结构上都在PowerShell中工作的脚本并且只在需要时调用x86代码会很棒.
有谁知道这样做的策略?
如果我Get-NetConnectionProfile在64位Powershell上运行,则可以正常工作。如果我在32位Powershell上运行它,则会出现以下错误:
Get-NetConnectionProfile : Provider load failure
+ CategoryInfo : NotSpecified: (MSFT_NetConnectionProfile:root/St
andardCi...nnectionProfile) [Get-NetConnectionProfile], CimException
+ FullyQualifiedErrorId : HRESULT 0x80041013,Get-NetConnectionProfile
Run Code Online (Sandbox Code Playgroud)
这是在具有Powershell 4.0版的64位Windows 8.1上。
我在REG_SZ使用以下方法获取值时遇到了一些奇怪的麻烦:
(get-itemproperty -Path "Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player" -Name UninstallString).UninstallString
(get-itemproperty -Path "Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player" -Name UninstallString).UninstallString
get-itemproperty : Cannot find path 'HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player' because it
does not exist.
At line:1 char:2
+ (get-itemproperty -Path "Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKEY_LOCAL_MACH...LC media player:String) [Get-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
Run Code Online (Sandbox Code Playgroud)
此方法适用于另一个REG_SZ没有问题的方法,但是当我在下面调用许多键时,Uninstall它将失败。
具体来说,它适用于:
(get-itemproperty -Path "Registry::HKEY_CURRENT_USER\Software\Microsoft\Command Processor" -Name autorun).AutoRun
这两个数据条目都在我的系统上存在,如regedit中所示。
但是,非常有趣的是,它们在以下结果中不存在:
Get-ChildItem "Registry::HKEY_LOCAL_MACHINE\software\microsoft\windows\currentversion\uninstall\"
Run Code Online (Sandbox Code Playgroud)
还有更多的“缺失”键。这似乎是我不熟悉的一些奇怪的注册表名称空间虚拟化(类似于HKEY_CLASSES_ROOT)?