Reputation: 13
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:
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"
}
}
Upvotes: 1
Views: 503
Reputation: 174920
$FileBrowser.ShowDialog()
returns the result of showing the dialog - not the file path that was actually selected.
For that, you'll need $FileBrowser.FileName
:
$dialogResult = $FileBrowser.ShowDialog()
if($dialogResult -eq 'OK'){
# user selected a file
$GetList = $FileBrowser.FileName
}
else{
# user canceled the dialog, either `return`, throw an error, or assign a static value to `$GetList`, like this
$GetList = "C:\default\path\to\hostnames.txt"
}
# Now we can safely proceed with `Get-Content`:
$SystemList = Get-Content -LiteralPath $GetList
Upvotes: 3