2

ローカル コンピューターとリモート コンピューターでサービスを開始および停止する PowerShell コマンドを使用して Web サービスを作成しています。

リモート コンピューターでサービスを開始および停止することは問題ではありません。以下に示すように、WmiObject を使用してこれを行います。

ローカル サービスを開始したい場合、権限がないと表示されます。

ローカル サービスを開始したい場合、資格情報で WmiObject を使用できません。

管理者権限でサービスを開始するにはどうすればよいですか?

私のスクリプト (strScriptText):

$username = "domain\administrator"
$pw = convertto-securestring "password" -asplaintext -force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $pw
$computername = "serverAB"
if ( $computername.Contains("serverAB")){(Get-WmiObject -class Win32_Service -filter "name='AppIDSvc'").startservice().returnvalue}
else {(Get-WmiObject -class Win32_Service -ComputerName $computername -Credential $cred -filter "name='AppIDSvc'").startservice().returnvalue}

動詞:

 runspace = RunspaceFactory.CreateRunspace()
        runspace.Open()
 pipeline = runspace.CreatePipeline()

pipeline.Commands.AddScript(strScriptText)
                pipeline.Commands.Add("Out-String")
4

1 に答える 1

0

PowerShell を介して古い .NET メソッドを使用することはできませんか。

# Create an authentication object
$ConOptions = New-Object System.Management.ConnectionOptions
$ConOptions.Username = "dom\jpb"
$ConOptions.Password = "pwd"
$ConOptions.EnablePrivileges = $true
$ConOptions.Impersonation = "Impersonate"
$ConOptions.Authentication = "Default"

# Creation of a rmote or local process
$scope = New-Object System.Management.ManagementScope("\\dom.fr\root\cimV2", $ConOptions)
$ObjectGetOptions = New-Object System.Management.ObjectGetOptions($null, 
                                                          [System.TimeSpan]::MaxValue, $true)
$proc = New-Object System.Management.ManagementClass($scope, 
                                      "\\dom.fr\ROOT\CIMV2:Win32_Process", $ObjectGetOptions)

# Equivalent to : 
# $proc = [wmiclass]"\\.\ROOT\CIMV2:Win32_Process"
# $res = $proc.Create("cmd.exe")
于 2012-10-17T12:26:05.620 に答える