将变量作为Powershell中的开关参数传递

问题描述 投票:5回答:1

我正在尝试创建一个Powershell脚本,该脚本将捕获所有已启用且处于非活动状态的Active Directory帐户90天。该脚本将提示用户在查询计算机或用户帐户之间进行选择。根据选择,它将作为变量传递给主命令。

如果我不传递变量,命令将正常工作。

我不确定我想做的事情是否可行。

对不起,任何错误的代码格式。刚开始。

Clear-Host
write-host "`nProgram searches for Enabled AD users account that have not logged in for more than 90 days. `nIt searches the entire domain and saves the results to a CSV file on users desktop." "`n"

$choice = Read-host -Prompt " What do you want to search for Computer or Users Accounts`nType 1 for users`nType 2 for Computers`n`nChoice"

$account
if ($choice -eq 1) {
    $account = UsersOnly
}
Elseif ($choice -eq 2) {
    $account = ComputersOnly
}
Else {
    write-host "This is not an option `n exiting program"
    exit
}

$FileName = Read-Host -Prompt "What do you want to name the CSV file"
$folderPath = "$env:USERPROFILE\Desktop\$FileName.csv"

Search-ADAccount -AccountInactive -TimeSpan 90 -$account | Where-Object { $_.Enabled -eq $true } | select Name, UserPrincipalName, DistinguishedName | Export-Csv -Path $folderPath
powershell active-directory
1个回答
5
投票

Splatting是实现此目的的方法。之所以这样命名,是因为您使用@而不是$引用了变量,并且@的类型看起来像是“飞溅”。

© www.soinside.com 2019 - 2024. All rights reserved.