Reputation: 31
Sorry if this is a basic question, I'm a PS beginner... How come the following Powershell function runs fine and writes output to the console when I add it to $Profile, but doesn't produce any output when I run it as a .ps1? (When I run as a script, it seems to run but doesn't return any output or error.
My execution policy = unrestricted and I'm running as administrator. Thoughts?
function Get-NetStats
{
$properties = 'Protocol','LocalAddress','LocalPort'
$properties += 'RemoteAddress','RemotePort','State','ProcessName','PID'
netstat -ano | Select-String -Pattern '\s+(TCP|UDP)' | ForEach-Object {
$item = $_.line.split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)
if($item[1] -notmatch '^\[::')
{
if (($la = $item[1] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6')
{
$localAddress = $la.IPAddressToString
$localPort = $item[1].split('\]:')[-1]
}
else
{
$localAddress = $item[1].split(':')[0]
$localPort = $item[1].split(':')[-1]
}
if (($ra = $item[2] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6')
{
$remoteAddress = $ra.IPAddressToString
$remotePort = $item[2].split('\]:')[-1]
}
else
{
$remoteAddress = $item[2].split(':')[0]
$remotePort = $item[2].split(':')[-1]
}
New-Object PSObject -Property @{
PID = $item[-1]
ProcessName = (Get-Process -Id $item[-1] -ErrorAction SilentlyContinue).Name
Protocol = $item[0]
LocalAddress = $localAddress
LocalPort = $localPort
RemoteAddress =$remoteAddress
RemotePort = $remotePort
State = if($item[0] -eq 'tcp') {$item[3]} else {$null}
} | Select-Object -Property $properties
}
}
}
Upvotes: 1
Views: 6692
Reputation: 37
You can try something like this
$myStaff = Get-NetStats
Write-output "$myStaff"
Upvotes: 0
Reputation: 516
In order to see output from a function that lives in a .ps1
file when running the .ps1
file, you need to add the function call inside the script.
For example, say I create the following file: Do-AlmostNothing.ps1
# Do-AlmostNothing.ps1
# define the function doNotSoMuch
function doNotSoMuch {
Write-Host "Look how little I've accomplished!"
}
# execute the function defined above
doNotSoMuch
Without the call to the function, nothing will happen.
Execute the script:
PS> ./Do-AlmostNothing.ps1
Look how little I've accomplished!
PS>
Upvotes: 1
Reputation: 10809
TessellatingHeckler got it right, although perhaps not quite as clearly as, and maybe a little more snarky than, I'd have put it:
Simply stated, your .ps1 defines the function Get-NetStats but it doesn't call the function. Without calling the function, you won't get any output from it.
Once you run the .ps1 - or, if the function definition is in your profile - the function will be defined, and can be called any time you need it within that PowerShell session simply by typing Get-NetStats.
Upvotes: 2
Reputation: 31
I found a procedure that helped me figure this out. It's located here:
In PowerShell, how do I define a function in a file and call it from the PowerShell commandline?
Now, I need to figure out how to deploy my new function to remote hosts, maybe via GPO or another PowerShell script...
Cheers!
Upvotes: 1
Reputation: 266
Are you sure you just don't need to output the return result in your script? When you run a function from $Profile in a powershell window the result is returned to that window. In a script this is not the case.
In the script try:
$result = Get-NetStats
$result
Upvotes: 0