Script Block looks like it works but doesnt

Welcome Forums General PowerShell Q&A Script Block looks like it works but doesnt

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
2 years, 9 months ago.

  • Author
    Posts
  • #42762

    Participant
    Points: 0
    Rank: Member

    Apologies if this shows up elsewhere, I looked through the first three or so pages and didn't see anything on it.

    Overall I am trying to delete user profiles from several terminal servers when a user leaves via the remprof.exe command.

    Full disclosure: I want to have a CSV that lists all of our term servers (for ease of editing) and a separate CSV that lists the users (haven't even gotten that far).

    Testing out just the piece that deletes the profile sans variables, I am already failing. If I try just the invoke command:

    invoke-command -ComputerName computerName -ScriptBlock {'C:\windows\system32\remprof.exe userAccountName'}
    

    it looks like it works in the PS window, but doesn't actually run the script on the remote computer. I can run literally the exact line 'C:\windows\system32\remprof.exe userAccountName' and it works fine.

    From there it gets progressively worse with the foreach and get-content not working; powershell complaining the computer name isn't valid

    $computers = get-content "term_servers.csv"
    
    
    foreach ($computer in $computers)
    {
    
        $adcompname = Get-ADComputer $computer
        
        invoke-command -ComputerName $adcompname -ScriptBlock {'C:\windows\system32\remprof.exe userAccountName'}
    
        write-host "$computer done."
    
    }
    
    

    Any thoughts would be much appreciated!!

    Thanks,
    TJ

  • #42766

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Keep in mind that a Remoting connection does not spin up a profile on the remote machine, and it runs under delegated authentication. It's possible that RemProf.exe simply won't work in that context.

  • #42832

    Participant
    Points: 58
    Rank: Member

    if you are trying to remove local profiles you might look at something Boe Prox (https://twitter.com/proxb) wrote a while ago:

    http://poshcode.org/2477

    I used it when I needed to manage profiles with Remote desktop servers.

The topic ‘Script Block looks like it works but doesnt’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort