gpt4 book ai didi

powershell - 将 PSCredential 作为参数传递给脚本

转载 作者:行者123 更新时间:2023-12-01 12:36:31 28 4
gpt4 key购买 nike

如何从另一个脚本调用一个脚本并将 PSCredential 作为参数传递?

我拥有的代码正在将 PSCredential 转换为带有 System.Management.Automation.PSCredential 的字符串。作为字符串值,所以它不起作用:

错误:

 Cannot process argument transformation on parameter 'cred'. Cannot convert the 
"System.Management.Automation.PSCredential" value of type "System.String" to type "System.Management.Automation.PSCredential".

代码:
function getCurrentSequenceNumber([System.Management.Automation.PSCredential]$cred)
{
$scriptPath = 'D:\Ave\A\Scripts\UploadPasswordToStorageTable\ReadTableEntity\Read-TableEntity.ps1'
$argumentList = "-SubscriptionName $SubscriptionName -StorageAccountName $StorageAccountName -TableName $TableName -partitionKey $partitionKey -rowKey $rowKey -Key $Key -CredentialName $CredentialName -cred $cred"
$currentSequenceNumber = Invoke-Expression "$scriptPath $argumentList"
return $currentSequenceNumber
}

$cred = Get-Credential
$currentSequenceNumber = getCurrentSequenceNumber $cred

最佳答案

这有效:

& D:\Ave\A\Scripts\UploadPasswordToStorageTable\ReadTableEntity\Read-TableEntity.ps1 -SubscriptionName $SubscriptionName -StorageAccountName $StorageAccountName -TableName $TableName -partitionKey $partitionKey -rowKey $rowKey -Key $Key -CredentialName $CredentialName -cred $cred

您不能在双引号中使用对象,因为它会将其序列化为字符串。

关于powershell - 将 PSCredential 作为参数传递给脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29203062/

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