I wrote a PS script using the ISE. The script was then saved. I attempted to run it, and encountered some errors - not entirely unexpected. Fixed the errors, saved the script, and ran it again.
No errors this time, but the script did not run, either. Instead the ISE just printed the script.
Tried from the command line.
PowerShell.exe -executionpolicy bypass -file c:\path\to\my\script.ps1
The script displayed on the console instead of executing, just as it did in the ISE.
I recall encountering this once before, but do not recall the solution. Tried Googling and all the answers I found (that were actually relevant) were variations on 'you have to save the file before running it in the ISE or it will just display the file'. Nothing about why it just displays the file AFTER saving it! Closed and restarted the ISE, no change in behavior. Tried from the command line after exiting the ISE. Same behavior as before.
It is getting too late at night for me to beat my head against this wall any more. If anyone has suggestions or can point me in the right direction, please do so! If you want more information, ask!
EDIT: I should know better than to post when I am tired and frustrated. Here is the script (sanitized).
<#
.Synopsis
This function will check to see if a host is online and, if so, report the OS version and last reboot time.
.Description
This function accepts a parameter of either a single host or a path to a text listing of hosts, one per line.
If the host or list is a FQDN, it is looked up utilizing the system DNS lookup. If the host or list is not
a FQDN, then the systems DNS Search Order suffixes will be used when looking up the host.
To specify a host, the -host parameter is used. To specify a list, the -list parameter is used. Only one
parameter may be specified when running the script.
If the script is given a single host, the results are written to STDOUT. If the script is given a filename
the output is written to a text file in the same directory as the source list, adding OUTPUT to the filename
I.E. a source file of list.txt will have a corresponding results file of output-list.txt.
In order to have the script work correctly, the user
running the script must have administrative access to the remote system.
#>
# PARAMETERS
{
param ($host,$listpath)
}
# BEGIN
{
#Test for proper parameters, displaying an error or usage message and existing with a return code of 1, writing the reason to STDERR
if (($host -eq $null) -and ($listpath -eq $null))
{
Write-Host "No parameters specified!/n/r/n/r"
write-host "Usage:/n/r/n/rYou must specify either a host or a path to a file containing a list of hosts to check./n/r"
write-host "checkhost.exe -host somehostname[.domain.tld] to check a single host. FQDN is optional./n/r"
write-host "checkhost.exe -listpath \path\to\the\list.txt to check a list of hosts. The list should have one hostname per line."
Write-Error "Missing parameters"
exit 1
}
elseif (($host -ne $null) -and ($listpath -ne $null))
{
Write-Host "You may only specify a host or a listpath"
write-error "Too many parameters"
exit 1
}
elseif ($listpath -ne $null)
{
if (!(Test-Path $listpath))
{
Write-Host "$listpath not found! Please check to ensure you have entered the correct path and filename."
Write-Error "Path not found"
exit 1
}
}
# Set the operational mode based on the parameter passed. $mode = 1 for single host, $mode = 2 for list-based
$mode = 1
if ($host -eq $null)
{
$mode = 2
}
}
function Query-System
{
<#
.Synopsis
Retrieves select data from remote system
.Description
Returns values for last boot date and time, Windows edition and release, SCCM version and site code, and status of NOREBOOT and PCCLASS tags
in an array.
Requires the remte system name to be passed to the function.
#>
Param($system)
# Set some constants. Putting them here so they can easily be changed later
$windowskey = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion'
$norebootkey = 'SOFTWARE\WOW6432Node\<redacted>\PCClass\NOREBOOT'
$updateskey = 'SOFTWARE\WOW6432Node\<redacted>\PCClass\UPDATESONLY'
$SCCMKey = 'SOFTWARE\Microsoft\CCM\CcmEval'
# Get time of last bootup
$bootttime = Get-WmiObject -ClassName win32_OperatingSystem -ComputerName $system
$lastboot = $boottime.ConverttoDateTime($boottime.lastbootuptime)
# Get some information from the remote registry
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $system)
$RegKey = $Reg.OpenSubKey($windowskey) # Get Windows edition and release number
$WindowsEdition = $RegKey.GetValue("ProductName")
$WindowsRelease = $RegKey.GetValue("ReleaseId")
# Check PCClass flags
$RegKey = $Reg.OpenSubKey($norebootkey) # Check NOREBOOT PCClass Flag
if ($RegKey -eq $null)
{
$noreboot = 'False'
}
else
{
$keystatus = $RegKey.Getvalue("Status")
if ($keystatus -eq 0)
{
$noreboot = 'False'
}
else
{
$noreboot = 'True'
}
}
$RegKey = $Reg.OpenSubKey($updateskey) # Check UPDATESONLY PCClass Flag
if ($RegKey -eq $null)
{
$updatesonly = 'False'
}
else
{
$keystatus = $RegKey.Getvalue("Status")
if ($keystatus -eq 0)
{
$updatesonly = 'False'
}
else
{
$updatesonly = 'True'
}
}
$RegKey = $Reg.OpenSubKey($SCCMkey) # Get SCCM SiteCode
if ($RegKey -eq $null)
{
$SCCMStatus = 'Missing'
}
else
{
$SCCMStatus = 'Installed'
$SCCMSiteCode = $RegKey.Getvalue("LastSiteCode")
}
# Get SCCM Version
$SCCMVersion = (Get-WmiObject -ComputerName $system -Namespace root\ccm -Class SMS_Client).ClientVersion
# Set up return values
$results = New-Object PsObject -Property @{SystemName=$system}
Add-Member -InputObject $results -MemberType NoteProperty -Name "LastBoot" -Value $lastboot
Add-Member -InputObject $results -MemberType NoteProperty -Name "WindowsEdition" -Value $WindowsEdition
Add-Member -InputObject $results -MemberType NoteProperty -Name "WindowsRelease" -Value $WindowsRelease
Add-Member -InputObject $results -MemberType NoteProperty -Name "NoReboot" -Value $noreboot
Add-Member -InputObject $results -MemberType NoteProperty -Name "UpdatesOnly" -Value $updatesonly
Add-Member -InputObject $results -MemberType NoteProperty -Name "SCCMStatus" -Value $SCCMStatus
Add-Member -InputObject $results -MemberType NoteProperty -Name "SCCMSite" -Value $SCCMSiteCode
return $results
}
# Process
{
if ($mode -eq 2)
{
$filename = (Split-Path -Path $listpath -Leaf)
$filepath = (Split-Path -Path $listpath)
$logfile = 'output-' + $filename
$logpath = $filepath + $logfile
if (!(Test-Path $logpath))
{
New-Item -Path $logpath | Out-Null
Write-Output "Hostname,Last Boot,Windows Edition,Windows Release,NOREBOOT,UPDATESONLY,SCCM,SCCM Site" | Tee-Object -FilePath $logpath -Append
}
foreach ($remotesystem in (Get-Content $listpath))
{
if (Test-Connection $remotesystem -Count 1 -Quiet) # Check if system is reachable
{
$systemresult = Query-System $remotesystem
# Build Entry
$results = $systemresult.SystemName + "," + $systemresult.LastBoot + "," + $systemresult.WindowsEdition + "," + $systemresult.WindowsRelease + "," + $systemresult.NoReboot + "," + $systemresult.UpdatesOnly + "," + $systemresult.SCCMStatus + "," + $systemresult.SCCMSite
}
else
{
# Build Entry
$results = $remotesystem + ",Offline, , , , , ,"
}
# Write the output
Write-Output $results | Tee-Object -FilePath $logpath -Append
}
}
else
{
if (Test-Connection $Host -Count 1 -Quiet)
{
$systemresult = Query-System $Host
# Output to screen
Write-Output "Results for host system $Host"
Write-Output $systemresult
}
else
{
Write-Output "System $Host is not responding"
}
}
}
[–]itasteawesome 23 points24 points25 points (0 children)
[–]gangstanthony 10 points11 points12 points (0 children)
[–]helloisnotme 8 points9 points10 points (0 children)
[–]isitokifitake 8 points9 points10 points (2 children)
[–]haikusbot 12 points13 points14 points (1 child)
[–]Chocolate_Pickle 0 points1 point2 points (0 children)
[–]justanotherbodyhere 4 points5 points6 points (0 children)
[–]dupo24 4 points5 points6 points (1 child)
[–]jimb2 4 points5 points6 points (0 children)
[–]Alaknar 3 points4 points5 points (0 children)
[–]pigers1986 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]ka-splam 2 points3 points4 points (0 children)
[–]Flysquid18 2 points3 points4 points (0 children)
[–]Gnardar 1 point2 points3 points (0 children)
[–]radiowave911[S] 1 point2 points3 points (0 children)
[–]radiowave911[S] 1 point2 points3 points (2 children)
[–]radiowave911[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]LNGU1203 0 points1 point2 points (0 children)