绝对!您只需将变量值'profilenametocleanup' 替换为变量引用即可!
PowerShell 中的变量是这样定义和分配的:
$username = "Some value or expression goes here"
要提示用户输入字符串值,我们可以使用Read-Host cmdlet:
$username = Read-Host -Prompt "Enter the username of the profile to remove!"
然后,在现有管道中:
# replace the 'profiletocleanup' value with our variable instead
Get-CimInstance -ComputerName servername1, servername2, servername3 -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('')[-1] -eq $username } | Remove-CimInstance
如果您以后可能想在不同的脚本中重用此管道,最好将其重写为函数:
function Remove-RemoteUserProfile
{
param(
[string]$Username,
[string[]]$ComputerName = @('servername1', 'servername2', 'servername3')
)
Get-CimInstance -ComputerName $ComputerName -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('')[-1] -eq $Username } | Remove-CimInstance
}
现在您可以使用相同的命令定位任何用户或一组远程服务器:
# Will remove user profile for user jdoe on servername1 through 3
Remove-RemoteUserProfile -User "jdoe"
# Will remove user profile for user jdoe on servername1 and a machine called client5
Remove-RemoteUserProfile -User "bob" -ComputerName servername1,client5
# In this example we prompt the user for the username before calling the function:
$username = Read-Host -Prompt "Enter the username of the profile to remove!"
Remove-RemoteUserProfile -User $username -ComputerName someOtherComputer123