Powershell: Windows サービスを使用してテキストファイルからセキュリティで保護された文字列を読み取る方法

Powershell: Windows サービスを使用してテキストファイルからセキュリティで保護された文字列を読み取る方法

Powershell を使用して vSphere 環境の資格情報をテキスト ファイルに保存しましたConvertFrom-SecureString。次のコマンドを使用して、Powershell スクリプトでこの資格情報を使用します。

$password4host = get-content C:\shutdown\HostsCred.txt | convertto-securestring

サービスを通じて PowerShell スクリプトを実行しようとすると、資格情報をテキスト ファイルから読み取れないというエラーが発生します。これは、セキュリティで保護された文字列をテキスト ファイルに保存することが、これらの資格情報をテキスト ファイルに保存したユーザーにバインドされているという事実に関係しているようです。資格情報を復号化できるのは、ユーザー自身だけです。

コードスニペット:

$time = ( get-date ).ToString('HH-mm-ss')
$date = ( get-date ).ToString('dd-MM-yyyy')
$logfile = New-Item -type file "C:\shutdown\ShutdownLog-$date-$time.txt" -Force
Import-Module -Name VMware.*
# Some variables
$vcenter = "10.10.10.10"
$username = "[email protected]"
$username4host = "fakeuser"
$cluster = "ESXi-Cluster"
$datacenter = "Datacenter"
$vCenterVMName = "VMware vCenter Server" #Name of vCenter VM
$StarWindVM1 = "Starwind-VM" #Name of first StarWind VM
$StarWindVM2 = "Starwind-VM (1)" #Name of second StarWind VM
$StarWind = "10.10.10.11" #IP address of one of StarWind VMs
$ESXIhost1 = "10.10.10.12" #Name of first ESXI Host
$ESXIhost2 = "10.10.10.13" #Name of second ESXI Host
try
{
    $password4vCenter = get-content C:\shutdown\vCenterCred.txt | convertto-securestring
}
catch
{
    Write-Host $_ -foreground red
    Add-Content $logfile "$(get-date -f dd/MM/yyyy) $(get-date -f HH:mm:ss) $_"
    exit
}
try
{
    $password4host = get-content C:\shutdown\vCenterCred.txt | convertto-securestring
}
catch
{
    Write-Host $_ -foreground red
    Add-Content $logfile "$(get-date -f dd/MM/yyyy) $(get-date -f HH:mm:ss) $_"
    exit
}
$tmp = get-content C:\Temp\HostsCred.txt | convertto-securestring
$password4Starwind = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($tmp)) 
$credentials = new-object System.Management.Automation.PSCredential $username, $password4vCenter
$credentials4host = new-object System.Management.Automation.PSCredential $username4host, $password4host
Write-Host ""
Write-Host "$(get-date -f dd/MM/yyyy) $(get-date -f HH:mm:ss) Shutdown command has been sent to the vCenter Server." -Foregroundcolor yellow
Write-Host "This script will shutdown all of the VMs and hosts located in $datacenter." -Foregroundcolor yellow
Write-Host ""
Sleep 5
Add-Content $logfile "$(get-date -f dd/MM/yyyy) $(get-date -f HH:mm:ss) PowerOff Script Engaged"
Add-Content $logfile ""
# Connect to vCenter
$counter = 0
if ($counter -eq 0){
       Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$false | Out-Null
}
Write-Host "Connecting to vCenter - $vcenter.... " -nonewline
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false
Add-Content $logfile "$(get-date -f dd/MM/yyyy) $(get-date -f HH:mm:ss) Connecting to vCenter - $vcenter"
try
{
    $success = Connect-VIServer $vcenter -Credential $credentials -WarningAction:SilentlyContinue
}
catch
{
    Write-Host $_ -foreground red
    Add-Content $logfile "$(get-date -f dd/MM/yyyy) $(get-date -f HH:mm:ss) $_"
    exit
}

しかし、このサービスのログオン ユーザーを、暗号化された資格情報を保存したユーザーにすでに変更しました。サービスがまだ特定のユーザーとして実行されていないか、Windows サービスでこれらのセキュリティで保護された文字列を使用できないようです。

そのユーザーでログインした状態で PowerShell ISE から PowerShell スクリプトを実行すると、エラーなしでスクリプトが実行されます。

PowerShell スクリプト内にプレーンテキストで保存せずに、Windows サービスを通じてトリガーされた PowerShell で資格情報を使用するにはどうすればよいでしょうか?

特定のユーザーによる Windows サービス

関連情報