尝试为远程工作站编写脚本删除脚本

时间:2018-06-07 20:36:22

标签: powershell

我在一家大公司工作,这是全球性的,我希望能够运行一个允许我远程清理旧配置文件的脚本。我找到了这个脚本,但似乎没有做任何事情。有没有人想过为什么。我是powershell的新手所以任何帮助将不胜感激。提前谢谢!

function Remove-ProfileWD {
<#   
.SYNOPSIS   
    Interactive menu that allows a user to connect to a local or remote computer and remove a local profile. 
.DESCRIPTION 
    Presents an interactive menu for user to first make a connection to a remote or local machine.  After making connection to the machine,  
    the user is presented with all of the local profiles and then is asked to make a selection of which profile to delete. This is only valid 
    on Windows Vista OS and above for clients and Windows 2008 and above for server OS.           
.EXAMPLE  
Remove-ProfileWF

Description 
----------- 
Presents a text based menu for the user to interactively remove a local profile on local or remote machine.    
#> 

#Prompt for a computer to connect to 
(
    [Parameter(Mandatory=$true)]
    $Computer = Read-Host "Please enter a computer name"
        )
#Test network connection before making connection 
If ($computer -ne $Env:Computername) { 
    If (!(Test-Connection -comp $computer -count 1 -quiet)) { 
        Write-Warning "$computer is not accessible, please try a different computer or verify it is powered on."
        Break
        } 
    }
Do {     
#Gather all of the user profiles on computer 
Try { 
    [array]$users = Get-WmiObject -ComputerName $computer Win32_UserProfile -filter "LocalPath Like 'C:\\Users\\%'" -ea stop

    } 
Catch { 
    Write-Warning "$($error[0]) "
    Break
    }     
#Cache the number of users 
$num_users = $users.count 

Write-Host -ForegroundColor Green "User profiles on $($computer):"
Write-Host -ForegroundColor Green "User profile          Last Use ate"

    #Begin iterating through all of the accounts to display 
    For ($i=0;$i -lt $num_users; $i++) { 
        Write-Host -ForegroundColor Green "$($i): $(($users[$i].localpath).replace('C:\Users\',''))     $(get-item \\$computer\C`$\users\$(($users[$i].localpath).replace('C:\Users\',''))|  Foreach {$_.LastWriteTime}) "
        } 
    Write-Host -ForegroundColor Green "q: Quit"
    #Prompt for user to select a profile to remove from computer 
    Do {     
        $account = Read-Host "Select a number to delete local profile or 'q' to quit"
        #Find out if user selected to quit, otherwise answer is an integer 
        If ($account -NotLike "q*") { 
            $account = $account -as [int]
            } 
        }         
    #Ensure that the selection is a number and within the valid range 
    Until (($account -lt $num_users -AND $account -match "\d") -OR $account -Like "q*") 
    If ($account -Like "q*") { 
        Break
        } 
    Write-Host -ForegroundColor Yellow "Deleting profile: $(($users[$account].localpath).replace('C:\Users\',''))"
    #Remove the local profile 
    ($users[$account]).Delete() 
    Write-Host -ForegroundColor Green "Profile:  $(($users[$account].localpath).replace('C:\Users\','')) has been deleted"

    #Configure yes choice 
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Remove another profile."

    #Configure no choice 
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Quit profile removal"

    #Determine Values for Choice 
    $choice = [System.Management.Automation.Host.ChoiceDescription[]] @($yes,$no) 

    #Determine Default Selection 
    [int]$default = 0 

    #Present choice option to user 
    $userchoice = $host.ui.PromptforChoice("","Remove Another Profile?",$choice,$default) 
    } 
#If user selects No, then quit the script     
Until ($userchoice -eq 1)
}

0 个答案:

没有答案