none
PowerShell Script to do a Specific Function

    Question

  • Hello All, I am a complete noob to PowerShell Scripting and am anxious to learn more.

    I need to create a PS script to perform the following functions in a single script and have no idea how. I have listed the required functions below, any help would be great.

    # Log into "ServerName" as Administrator
    # Open a command prompt:
    # Run DOS Command - net user "username" /add /random
    # Log off Server
    # Log into "AnotherServerName" as Administrator
    # Open a Command Prompt:
    # Run DOS Command - net user "username" /add /random
    # Then go to "c:\FolderName\FolderName" and add the "UserName" to "TextFileHere.txt" text file and Save it.
    # Then in the same directory, Run the "AGreatVBSScript.vbs" script

    Thanx

    Tuesday, May 18, 2010 6:56 AM

Answers

  • Larry;

    You can do all tihis via WMI

    Something like this might get you going:

    $process = ([WMICLASS]"\\$server\ROOT\CIMV2:Win32_Process").Create("cmd.exe /c net user 'username' /add /random") 
    #Wait for the process to end: 
    [string]$ID = $process.ProcessID 
    Write-Output "Waiting for process $ID to finish on $server" 
    while (test-process $process.ProcessId $server) { } # Wait for terminate 
    $process = ([WMICLASS]"\\$server\ROOT\CIMV2:Win32_Process").Create("cscript.exe /c c:\FolderName\FolderName\AGreatVBSScript.vbs") 
    [string]$ID = $process.ProcessID 
    Write-Output "Waiting for process $ID to finish on $server" 
    while (test-process $process.ProcessId $server) { } # Wait for terminate 
    "administrator" |out-file \\$server\c$\FolderName\FolderName -append

    Karl


    http://unlockpowershell.wordpress.com
    Email me@ [regex]::Matches("107097114108109105116115099104107101064109116046110101116",'(.{3})')|%{$m+=[char][int]$_.value};$m
    Tuesday, May 18, 2010 2:38 PM

All replies

  • Larry;

    You can do all tihis via WMI

    Something like this might get you going:

    $process = ([WMICLASS]"\\$server\ROOT\CIMV2:Win32_Process").Create("cmd.exe /c net user 'username' /add /random") 
    #Wait for the process to end: 
    [string]$ID = $process.ProcessID 
    Write-Output "Waiting for process $ID to finish on $server" 
    while (test-process $process.ProcessId $server) { } # Wait for terminate 
    $process = ([WMICLASS]"\\$server\ROOT\CIMV2:Win32_Process").Create("cscript.exe /c c:\FolderName\FolderName\AGreatVBSScript.vbs") 
    [string]$ID = $process.ProcessID 
    Write-Output "Waiting for process $ID to finish on $server" 
    while (test-process $process.ProcessId $server) { } # Wait for terminate 
    "administrator" |out-file \\$server\c$\FolderName\FolderName -append

    Karl


    http://unlockpowershell.wordpress.com
    Email me@ [regex]::Matches("107097114108109105116115099104107101064109116046110101116",'(.{3})')|%{$m+=[char][int]$_.value};$m
    Tuesday, May 18, 2010 2:38 PM
  • Thanks Karl,

    I will give it a shot and let you know how I go.

    Cheers

    Larry.

    Tuesday, May 18, 2010 10:26 PM
  • Do you know why the following doesnt actually wait for the process to exit?  It returns the PID and just ends without waiting.

    $process = ([WMICLASS]"\\$computer\ROOT\CIMV2:win32_process").Create("notepad")
    [string]$ID = $process.ProcessID 
    Write-Output "Waiting for process $ID to finish on $computer" 
    while (test-process $process.ProcessId $computer) { } # Wait for terminate 

    Friday, June 07, 2013 7:32 PM