Hi there.
I am trying to get the unc path from servers in file in this script to work but I can’t for some reson. The param $serverName is empty. Some help please.
$serverFiles = @(
"C:\Scripts\Antura\Servers\allServersGFTest.txt"
)
$servers = @()
foreach ($file in $serverFiles) {
$servers += Import-Csv -Path $file
}
foreach ($serverInfo in $servers) {
$option = New-PSSessionOption -IncludePortInSPN
$pssession = New-PSSession -ComputerName $serverInfo.Name -SessionOption $option
$scriptPath = Invoke-Command -Session $pssession -ScriptBlock {
param (
$serverName
)
$uncPath = "\\$serverName\c`$\Scripts\DailyMonitor\*.ps1"
$scriptPath = Get-ChildItem $uncPath -Recurse -Depth 0 -File |
Where-Object { $_.Name -notlike '*Copy*' -and $_.Name -notlike '*original*' } |
Select-Object -First 1
if ($scriptPath) {
$scriptPath.FullName
}
} -ArgumentList $serverInfo.Name
if ($scriptPath) {
# Start PowerShell script
Invoke-Expression -Command $scriptPath
} else {
Write-Host "No PowerShell script (*.ps1) found (excluding those with 'Copy') in the specified path on $($serverInfo.Name)."
}
# Remove the session inside the loop
Remove-PSSession $pssession
}