我无法在Start-Job中将数组传递给scriptblock.你能告诉我我可能做错了什么吗?
$bounceBlock = {
param(
[string[]]$list,
[System.Management.Automation.PSCredential]$cred
)
Add-PSSnapin VMware.VimAutomation.Core | Out-Null
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
Connect-VIServer -Server servername -Credential $cred -AllLinked
Get-VM -Name $list
}
if ($targets) {
$activeTargets = $targets | Get-Random -Count $prodTargets.Count
$counter = [pscustomobject] @{Value = 0}
$groupSize = 50
$groups = $activeTargets | Group-Object -Property {[math]::Floor($counter.Value++ / $groupSize)}
$connection = Connect-VIServer -Server servername -Credential $cred -AllLinked
if ($connection -match "servername") {
foreach ($group in $groups) {
while ((Get-Job …Run Code Online (Sandbox Code Playgroud) 我为我的管理组创建了一个模块,其中包含一些自动执行我们常用程序的功能(将管理员添加到远程计算机,C驱动器清理等等)
这些功能的先决条件之一是生成一系列7个凭证,每个凭据用于我们工作的每个域.
有没有办法在导入模块时让脚本块运行,或者这是我应该添加到每个人员配置文件的东西?
一位评论者提到我可以将它添加到module.psm1文件中,但这不起作用.这是我试图运行的代码.
$creds = Import-Csv [csvfile]
$key = Get-Content [keyfile]
foreach ($cred in $creds) {
$user = $cred.User
$password = $cred.Hash | ConvertTo-SecureString -Key $key
$i = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$password
New-Variable -Name ($cred.Domain + "_Cred") -Value $i -Force
}
Run Code Online (Sandbox Code Playgroud)
手动运行此工作正常,但从Import-Module命令运行时不会创建凭据.
我在使用Export-Csv工作时遇到了一些困难.我正在创建一个像这样的数组......
[pscustomobject] @{
Servername = $_.Servername
Name = $_.Servername
Blk = ""
Blk2 = ""
Method = "RDP"
Port = "3389"
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是当我尝试将其导出为CSV时,我会看到像这样的垃圾......
"9e210fe47d09416682b841769c78b8a3" ,,,,,
我已经阅读了大量有关此问题的文章,但我只是不明白如何正确获取数据.
我想知道是否可以使用命令行调用在 PowerShell 脚本中使用的变量。
例如,我想在今晚运行一个脚本。我想每次使用不同的输入文件(csv)运行它两次。
我可以打电话吗...
powershell.exe -command {$input = <csv path>} -file <script path>
Run Code Online (Sandbox Code Playgroud)
并从脚本外部离散地调用脚本中使用的变量?我是否只需要制作脚本的两个副本,其中包含在脚本本身中的变量更改?有没有办法做到这一点,我失踪?