Posted On July 12, 2022

PowerShell: Running Commands on Remote Computers

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> PowerShell: Running Commands on Remote Computers
# runCommandsOnRemoteComputers.ps1

# User defined variables
$computernames=@(
'SERVER001',
'SERVER002'
)
$expectedExecutable='racadm.exe'
$expectedInstallPath='C:\program files\Dell\SysMgt\iDRACTools\racadm'

# Execution
foreach($computername in $computernames){
	invoke-command -computername $computername -scriptblock{
	param($expectedInstallPath,$expectedExecutable)
		$commandAvailable=try{get-command $expectedExecutable -EA Ignore}catch{$false}
		$proceed=if(!$commandAvailable){
			$environmentalPathExists=$env:path -like "*$expectedInstallPath*"
			if(!$environmentalPathExists){
				$env:path+=";$expectedInstallPath"			
			}
			$null=RefreshEnv
			$commandNowAvailable=try{get-command $expectedExecutable -EA Ignore}catch{$false}
			if($commandNowAvailable){$true}else{$false}
		}
		if($proceed){
			write-host "Configuring iDrac on $env:computername"
			racadm set iDRAC.NIC.DNSRacName ($env:computername).tolower()
			racadm set iDRAC.NIC.DNSRegister 0
			racadm set iDRAC.IPv4.DHCPEnable 1
			racadm set iDRAC.IPv4.DNSFromDHCP 1
			racadm set iDRAC.NIC.DNSDomainFromDHCP 1
			racadm set iDRAC.NIC.VLanEnable 0
		}
	} -Args $expectedInstallPath,$expectedExecutable
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How to Setup Dynamic DNS with Google Domains & Ubiquity EdgeRouter

Step 1: Set up Dynamic DNS - Access Google Domains: - Click on the Manage…

How to Install & Configure Pihole on Ubuntu 20.04

1. Installation- Run these commands: # sudo apt-get install gamin -ysudo curl -sSL https://raw.githubusercontent.com/pi-hole/pi-hole/master/automated%20install/basic-install.sh |…

Regular Expression (RegEx) Overview

A good online tool to test your regex skills: https://regexr.com/ Syntax: 1. /pattern/flags2. New RegExp(pattern,…