This is my first Powershell command and it has been a week and I can't figure out what's the problem. What I want to do is the following:
- Select a TXT or CSV file contains a list of hostname
- Store in a variable
- Later I am going to run a loop command for each system
My first issue is that for some reason my Get-Content command seems not to be working,
#Select File that contains the list of machines
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [Environment]::GetFolderPath('Desktop')
Filter = 'Text File (*.txt)|*.docx|Spreadsheet (*.csv)|*.csv|All Files (*.*)|*.*'
}
$GetList = $FileBrowser.ShowDialog()
#Get Each List of System
$SystemList = Get-Content -Path "$GetList"
Eventually, I am going to run a command calling $SystemList variable
# Remote run the install for each system
foreach ($System in $SystemList) {
if (test-Connection -Cn $System -quiet) {
Copy-item $SetupFolder -Destination \$System$Dest -recurse -Force
if (Test-Path - Path $) {
Invoke-Command -ComputerName $System -ScriptBlock {powershell.exe $Path /S} -credential $Credentials
Write-Host -ForegroundColor Green "Installation Successful on $System"
}
} else {
Write-Host -ForegroundColor Red "$System is not online, Install failed"
}
}
question from:
https://stackoverflow.com/questions/66067701/get-hostname-from-txt-or-csv-store-in-variable 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…