Powershell 将PSCredential作为参数传递给脚本

Powershell 将PSCredential作为参数传递给脚本,powershell,Powershell,如何从另一个脚本调用一个脚本并将PSCredential作为参数传递 我的代码正在将PSCredential转换为字符串,字符串值为System.Management.Automation.PSCredential,因此无法工作: 错误: Cannot process argument transformation on parameter 'cred'. Cannot convert the "System.Management.Automation.PSCredential" value

如何从另一个脚本调用一个脚本并将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".
代码:


调用表达式接受字符串并将其作为脚本块运行,因此您可以有效地将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
这起到了作用:

& 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

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

我认为,因为您将$cred放在引号中,它仍然将其转换为字符串。这也给了我这个错误:&:术语D:\Ave\a\Scripts\UploadPasswordToStorageTable\ReadTableEntity\Read-TableEntity.ps1-SubscriptionName$SubscriptionName-StorageAccountName$StorageAccountName-TableName$StorageAccountName-partitionKey$rowKey$Key-CredentialName$cred$cred'不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
& 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