如何从另一个脚本调用一个脚本并将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
答案 0 :(得分:2)
这有效:
& 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
您不能在双引号中使用对象,因为它会将其序列化为字符串。
答案 1 :(得分:0)
Invoke-Expression接受一个字符串并将其作为一个脚本块运行,因此您可以有效地将PSCredential对象转换为字符串,而不是将其作为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 = & $scriptPath $argumentList
return $currentSequenceNumber
}
$cred = Get-Credential
$currentSequenceNumber = getCurrentSequenceNumber $cred