gpt4 book ai didi

powershell - 如何传递凭据来重命名命令?

转载 作者:行者123 更新时间:2023-12-03 00:39:48 25 4
gpt4 key购买 nike

我在 PowerShell 脚本中运行以下命令来简单地重命名计算机。该脚本将由计算机启动脚本 GPO 执行,因此我需要在命令中传递凭据。因为如果脚本在启动时执行,我看不到它发生了什么我正在以普通用户身份登录时运行脚本来测试它

(Get-WmiObject win32_computersystem).Rename( $NewName,'Password','domain\username')

该命令返回“5”的 ReturnValue - 拒绝访问。如何传递用户名和密码? (我了解脚本中密码的安全风险)
__GENUS          : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 5
PSComputerName :

最佳答案

如果您总是在同一台机器上运行它或关联的帐户漫游,那么 IIRC 您可以依靠 DPAPI 来存储 key ,如下所示:

# Capture once and store to file
$passwd = Read-Host "Enter password" -AsSecureString
$encpwd = ConvertFrom-SecureString $passwd
$encpwd
$encpwd > $path\password.bin

# Later pull this in and restore to a secure string
$encpwd = Get-Content $path\password.bin
$passwd = ConvertTo-SecureString $encpwd

# Extract a plain text password from secure string
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwd)
$str = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$str

如果这不起作用,您可以使用这种方法,但它不如上述方法安全:
$key = 1..32 | ForEach-Object { Get-Random -Maximum 256 }
$passwd = Read-Host "Enter password" -AsSecureString
$encpwd = ConvertFrom-SecureString $passwd -Key $key
$encpwd

# Could easily modify this to store username also
$record = new-object psobject -Property @{Key = $key; EncryptedPassword = $encpwd}
$record
$record | Export-Clixml $path\portablePassword.bin

# Later pull this in and restore to a secure string
$record = Import-Clixml $path\portablePassword.bin
$passwd = ConvertTo-SecureString $record.EncryptedPassword -Key $record.Key

# Extract a plain text password from secure string
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwd)
$str = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$str

关于powershell - 如何传递凭据来重命名命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13842611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com