如何从另一个脚本调用一个脚本并传递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
您不能在双引号中使用对象,因为它将序列化为字符串。