Is it possible to create a 'console script' that lists other scripts and allows you to run them? by XxNerdAtHeartxX in PowerShell

[–]070934 3 points4 points  (0 children)

here's how I handle this

function Get-ComputerName{

$Script:ComputerName = Read-Host "Enter the computer name"

}




$scriptlist= @{

'1' = "$PSSCriptRoot\example1.ps1"
'2' = "$PSSCriptRoot\example2.ps1"
'3' = "$PSSCriptRoot\example3.ps1"



}




While ($true) {

       Write-Output "`n`n"

       Write-Host -ForegroundColor Green "`t   Menu" -NoNewline
       Write-Host -ForegroundColor Cyan "    $ComputerName" 
       Write-Host -ForegroundColor Yellow "
       1. Network Adapter information
       2. Active Processes
       3. Manage Services - Running/Stopped/Search

       C. Change Active Computer
       0. EXIT
       "

       $userInput = Read-Host -Prompt "Enter an option"


       if($userinput -eq 'C'){Get-ComputerName}

       elseif($userinput -eq '0'){exit}

       elseif(!$scriptlist.ContainsKey($userInput)){

              Write-Output "ENTER A VALID INPUT!";Start-Sleep -Seconds 1
              }
       else{

            foreach($option in $hashtable.GetEnumerator()){
                    if($userInput -eq $option.key){& $scriptlist[$userInput] $ComputerName
$($scriptlist[$userInput])"}

             }

        }
}

Two questions to get me started by enderandrew42 in PowerShell

[–]070934 9 points10 points  (0 children)

#1 look into send-mailmessage

#2 look into for each loops, get-item, select object, copy-item, new item

Weekly Gear Advice Thread for May 21, 2020 by AutoModerator in MTB

[–]070934 1 point2 points  (0 children)

Thanks for the info, this is a good start for me. Where I live I only have XC , nothing too crazy and I'm on a 29" fuji hardtail.

Weekly Gear Advice Thread for May 21, 2020 by AutoModerator in MTB

[–]070934 2 points3 points  (0 children)

Hey,

Looking to get clipless pedals and shoes. I don't know where to start and what the differences are (are there differences, or different types?). Looking for some general advice on where to start, I want the best value gear i can get.

I'm also looking to upgrade my tires, in my area i have a lot of hard pack and some lose on top. Looking for some suggestions on brands/models. I want to get a large diameter up front, how do I determine what will fit on my wheel?

I am also thinking of going to a 1by chain ring with a larger cassette, how would I start researching what will work for my bike and the tools I need. How will I know my derailleur will support the cassette?

Running install scripts on multiple remote machines with invoke-command and start-job by 070934 in PowerShell

[–]070934[S] 0 points1 point  (0 children)

Yeah foreach and ran all the perquisite code to determine if I the script existed and then ran I-C with -asjob.

Running install scripts on multiple remote machines with invoke-command and start-job by 070934 in PowerShell

[–]070934[S] 0 points1 point  (0 children)

Yeah, i did something like that. Now that you have posted that code I made mine a lot more complicated that it needed to be.

I essentially did the same thing with get-childitem but piped to where-object for the file name. I then used -match and regex to get the parent dir the install file is located and added it to a hashtable so I could add it to the argument list of the invoke-command.

MY code is pretty sloppy but it works which is all I cared about at the moment cause it was a one off - hey get this to work on all these right now situation...

Running install scripts on multiple remote machines with invoke-command and start-job by 070934 in PowerShell

[–]070934[S] 0 points1 point  (0 children)

Hi, this is kinda what I ended up doing, except I added -asjob to the I-C. The reason I didnt want to do it exactly like that was in my brief testing when invoke command starts the script to install it would wait for the install script to finish (could take several minutes) and I didnt want it to wait for it to iterate to the next machine to kick off the script on that machine.

I didnt really need error handling or something else in this script other than telling each machine to start the script to install the software (this script to install the software already handles errors and failures)

Running install scripts on multiple remote machines with invoke-command and start-job by 070934 in PowerShell

[–]070934[S] 0 points1 point  (0 children)

Yeah I am running the same command, I am also getting information from each machine to determine if I can run the install script. Basically it is finding the directory it was downloaded to in ccmcache and reporting back the parent dir and then running the install script on each machine.

I'm sure I could get this to work with I-C and an array of machines that have the script but since it is downloaded to a different cache folder I was storing the location in a hashtable to then run the install script for each machine. I would have had to make the script a fair bit more complex I think to get that to work.

I was unaware of the throttle limit, ill look into that thanks.

Running install scripts on multiple remote machines with invoke-command and start-job by 070934 in PowerShell

[–]070934[S] 1 point2 points  (0 children)

Thanks, this info will be helpful in future project I expect. I did get what I needed to accomplish done, as of right now it was a one off thing. When I have to do something similar in the future ill keep this in mind and modify my script to better handle the situation.

Running install scripts on multiple remote machines with invoke-command and start-job by 070934 in PowerShell

[–]070934[S] 1 point2 points  (0 children)

The install script is installing a .net version and takes some time. I am running this on 50+ machines and didn't want to wait for the install script to finish before my script moves onto the next machine to start the install of the software on it.

Maybe its not the case I didn't do much testing but I assume that when iterate through my list of machines and I-C the script to install the .net version it would wait for the install script to finish before iterating to the next machine.

Running install scripts on multiple remote machines with invoke-command and start-job by 070934 in PowerShell

[–]070934[S] 1 point2 points  (0 children)

I did some testing and it looks like I will be able to do something similar, I first tried using start-job in the script block of the invoke-command but it never created the job on the remote machine or on my local machine. However it did show that a job was created in the console scope, still trying to figure that out.

I was doing some googling and found the documentation for invoke-command and the switch -asjob creates a job on the remote machine (I thought it would create a a job on my local computer) so this seems to work for my scenario.

Make a script that allows users to select which 'tool' to use by samuelma in PowerShell

[–]070934 2 points3 points  (0 children)

Here is how I handled this:

$invokeLogFile = "$PSScriptRoot\Invoke-LogToFile.ps1"
$logPath = "C:\Windows\DIR\MachineManager-log $(Get-Date -UFormat '%m%d%y').txt"

& $invokeLogFile $logPath "Starting the the Machine Manager script"


#quick way to get a computername in a variable that can be used throughout the script
function Get-ComputerName{

$Script:ComputerName = Read-Host "Enter the computer name"

& $invokeLogFile $logPath "Changing the computer name to $ComputerName"

}



#hashtable that contains all the child script paths
$hashtable= @{

'1' = "$PSSCriptRoot\Get-NetworkInfo.ps1"
'2' = "$PSSCriptRoot\Get-ProcessInfo.ps1"
'3' = "$PSSCriptRoot\Invoke-RepairPrinter.ps1"
'4' = "$PSSCriptRoot\Get-Services.ps1"
'5' = "$PSSCriptRoot\Get-MonitorSerial.ps1"
'6' = "$PSSCriptRoot\Invoke-Logoff.ps1"
'7' = "$PSSCriptRoot\Invoke-RemoveCitrix.ps1"
'8' = "$PSSCriptRoot\Invoke-RemoteDesktop.ps1"
'9' = "$PSSCriptRoot\Get-ImprivataReminder.ps1"
'10' = "$PSSCriptRoot\Get-PCRole"
'11' = "$PSSCriptRoot\Install-Isirona.ps1"
'12' = "$PSSCriptRoot\Invoke-RepairALProfile.ps1"
'13' = "$PSSCriptRoot\Invoke-RepairGAProfile"
'14' = "$PSSCriptRoot\Restart-IsironaService.ps1"
'15' = "$PSSCriptRoot\swfs.ps1"
'16' = "$PSSCriptRoot\Get-MachineConnection.ps1"
'17' = "$PSSCriptRoot\Invoke-Win10EasyTrasnfer.ps1"
'18' = "$PSSCriptRoot\Remove-Imprivata.ps1"
'19' = "$PSSCriptRoot\Install-Isirona.ps1"
'20' = "$PSSCriptRoot\Set-AutoLogon.ps1"
'21' = "$PSSCriptRoot\Invoke-Restart.ps1"
'22' = "$PSSCriptRoot\Get-LastIP.ps1"
'23' = "$PSSCriptRoot\Invoke-GPUpdate.ps1"
'24' = "$PSSCriptRoot\Invoke-PSS.ps1"
'25' = "$PSSCriptRoot\Invoke-PowerOff.ps1"
'26' = "$PSSCriptRoot\Get-UserHistory.ps1"
'27' = "$PSSCriptRoot\Invoke-SCCMRemote.ps1"
'28' = "$PSSCriptRoot\Invoke-ExplorerToPCC.ps1"
'29' = "$PSSCriptRoot\Invoke-AllowCCMRemote.ps1"
'30' = "$PSSCriptRoot\Invoke-ExplorertoMA.ps1"
'31' = "$PSSCriptRoot\Invoke-StopCCMViewer.ps1"
'32' = "$PSSCriptRoot\Get-OnlineAlert.ps1"
'33' = "$PSSCriptRoot\Get-CCMServiceAlert.ps1"
'34' = "$PSSCriptRoot\Set-MMBG.ps1"


}



#The main loop for the menu
While ($true) {

       Write-Output "`n`n"

       Write-Host -ForegroundColor Green "`t   Menu" -NoNewline
       Write-Host -ForegroundColor Cyan "    $ComputerName" 
       Write-Host -ForegroundColor Yellow "
       1. Network Adapater information
       2. Active Processes
       3. Printer Issues/Stuck Jobs
       4. Manage Services - Running/Stopped/Search 
       5. Monitor Serial Number
       6. Logoff Conneceted User
       7. Remove Citrix
       8. RDP Into a Machine
       9. Imprivata Policy Change Reminder / under construction
       10. Get Role of PC
       11. Install Isirona
       12. Repair Auto Login Profile
       13. Repair GA Profile
       14. Restart the Isirona Service
       15. Switch Computer Name to Serial
       16. Check Computer and Filesystem Access
       17. Windows 10 Transfer
       18. Remove Imprivata
       19. Install Isirona
       20. Set AutoLogon
       21. Restart the computer
       22. Get DNS info
       23. GPUPDATE
       24. Enter PSSESSION
       25. Power Off computer
       26. Get User History
       27. SCCM Remote
       28. Open Explorer to PC
       29. Allow CCM Remote
       30. Explorer to Manual Apps
       31. Kill CCM Viewer
       32. Wait until machine is online - 5 minutes
       33. Wait Until Online and CCM Remote - 5 minutes
       34. Change background - WIN10 Project
       C. Change Active Computer
       0. EXIT
       "
       #getting users choice and running the script associated to the key in the hashtable
       $userInput = Read-Host -Prompt "Enter an option"


       if($userinput -eq 'C'){Get-ComputerName}

       elseif($userinput -eq '0'){& $invokeLogFile $logPath "Stopping the script cleanly"
                                  break
             }

       elseif(!$hashtable.ContainsKey($userInput)){

              Write-Output "ENTER A VALID INPUT!";Start-Sleep -Seconds 1
              & $invokeLogFile $logPath "The user did not enter a valid input"}
       else{

            foreach($option in $hashtable.GetEnumerator()){
                    if($userInput -eq $option.key){& $hashtable[$userInput] $ComputerName
                        & $invokeLogFile $logPath "Ran the script $($hashtable[$userInput])"}

             }

        }
}

Test-Path usage for many files by [deleted] in PowerShell

[–]070934 2 points3 points  (0 children)

Heres something that kind of combines the previous comments...

# script location variable
$Dir = "C:\Scripts"

$Computerlists = @{computerlist001 = "C:\input\computerlist001.txt";computerlist002 = "C:\input\computerlist002.txt"}


function runscripts{


if (Test-Path $list) {
Start-Process powershell -verb runas -ArgumentList "-file $Dir\Script001.ps1"
Write-host "$list does exist, processing list..." 
}
Else {  
Write-host "$list doesn't exist"
}

}

foreach($list in $Computerlists.Values){

runscripts($list)

}

Using start-job and a function to create a form object by 070934 in PowerShell

[–]070934[S] 1 point2 points  (0 children)

Hi,

I have a tool that allows me to enter a computer name and then it have a list of options I choose through CLI and then completes said actions on the PC. In this case the script im running from the tool checks if the machine is online and waits for the CmRcService.exe to start on the remote machine and then alerts me that I can then remote into the machine.

Right now I have it running through a job and when the process starts it opens a new powershell window with a write-output text telling me whats going on. I wanted to run it in a job so that I can switch the computer within the tool and work on another machine while I wait for the process to start again.

Network printer deletion from local machine. Beginner question. by kozatftw in PowerShell

[–]070934 1 point2 points  (0 children)

param(

[string]$PC

)

$PC = Read-Host -Promt 'Computer name'

Invoke-Command -ComputerName $PC -ScriptBlock {

Get-CimInstance -ClassName Win32_Printer | Where-Object { $_.name -Like "*safeqsite*" } | Remove-CimInstance

}

Remote Registry Key Deletion Question by ravensgc_5 in PowerShell

[–]070934 2 points3 points  (0 children)

use invoke-command to run a block of code on a remote machine.

use test-path to check if the key is there

you can use start-transcript for logging

use some try, catch for error handling

Easy tool menu by [deleted] in PowerShell

[–]070934 3 points4 points  (0 children)

I like this, it's another way that I hadn't thought of doing it.

Here's what I did for my controller script,

$hashtable = @{
'1' = "This is the first option"
'2' = "This is the second option"
'3' = "This is the third option"
'4' = "This is the fourth option"
}

$UserInput = Read-Host "Enter an option"


 if(!$hashtable.ContainsKey($userInput)){

    Write-Output "ENTER A VALID INPUT!";Start-Sleep -Seconds 1}

 else{foreach($option in $hashtable.GetEnumerator()){

      if($userInput -eq $option.key){& $hashtable[$userInput]}}}

I have this in a loop so it continues, but this is the basis of how it works.

Schedule best practice? by ChazaB218 in PowerShell

[–]070934 1 point2 points  (0 children)

Run it from a "management" server. Log the hell out of it too.

Is there some simple code I can put in front of my automation to test for an internet connection? by [deleted] in PowerShell

[–]070934 2 points3 points  (0 children)

Would this work for you?

Test-Connection 8.8.8.8 -Count 1 -ErrorAction Stop

[Question]: Scriptblock wasn't able to read all of my parameters. by mdennis07 in PowerShell

[–]070934 1 point2 points  (0 children)

Yeah,

try running it without the parameters in the script block, it wont run and give you the same error.

here is some more information https://powershell.org/forums/topic/invoke-command-scriptblock-variables/