Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除cim实例powershell scrip(以正确的方式删除服务器配置文件)问题_Powershell_Profile_Remote Desktop_User Profile - Fatal编程技术网

删除cim实例powershell scrip(以正确的方式删除服务器配置文件)问题

删除cim实例powershell scrip(以正确的方式删除服务器配置文件)问题,powershell,profile,remote-desktop,user-profile,Powershell,Profile,Remote Desktop,User Profile,我正在使用此脚本以正确的方式从我的RD服务器中删除损坏的配置文件。我想知道是否可以让脚本要求清除配置文件名,而不是手动在脚本中键入它 Get-CimInstance -ComputerName servername1, servername2, servername3 -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq 'profilenametocleanup' } | Remove-CimIn

我正在使用此脚本以正确的方式从我的RD服务器中删除损坏的配置文件。我想知道是否可以让脚本要求清除配置文件名,而不是手动在脚本中键入它

Get-CimInstance -ComputerName servername1, servername2, servername3 -Class Win32_UserProfile | 
Where-Object { $_.LocalPath.split('\')[-1] -eq 'profilenametocleanup' } | 
Remove-CimInstance
该脚本运行良好,还可以删除损坏的注册表项。我想让我们的服务台使用它,这样做会让它更加业余友好


谢谢

绝对是!您只需要用变量引用替换变量值
'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
}
现在,您可以使用相同的命令以任何用户或远程服务器集为目标:

#将删除servername1到servername3上用户jdoe的用户配置文件
删除RemoteUserProfile-用户“jdoe”
#将删除servername1和名为client5的计算机上用户jdoe的用户配置文件
删除RemoteUserProfile-用户“bob”-ComputerName服务器名1,客户端5
#在本例中,我们在调用函数之前提示用户输入用户名:
$username=读取主机-提示“输入要删除的配置文件的用户名!”
删除RemoteUserProfile-User$username-ComputerName someOtherComputer123

本地路径并不总是与用户名完全匹配。使用sid更可靠。事实上,该对象是由sid索引的

$user = 'admin'
$sid = Get-LocalUser $user | foreach { $_.sid.value }
get-wmiobject win32_userprofile | where sid -eq $sid | Remove-WmiObject -whatif

What if: Performing the operation "Remove-WmiObject" on target
"\\COMP001\root\cimv2:Win32_UserProfile.SID="S-2-6-31-4961843708-2576926490-3901110831-1002""

还有一个问题:Mathias R.Jessen。如何编辑脚本,使其在不调用过程的情况下提示输入用户名?是这样的:Get-CimInstance-ComputerName servername1、servername2、servername3-Class Win32_UserProfile | Where Object{$\u.LocalPath.split(“”)[-1]-eq$username=Read Host-Prompt“输入要删除的配置文件的用户名!”)|移除-CimInstance@LEVD您需要将
读取主机
包装在表达式中,以便像这样内联使用它:
{$something-eq(读取主机-提示“输入用户名”)}
(注意
读取主机
调用周围的括号)