How to format output in Posh Server (Powershell web server)? - powershell

I am currently trying the Powershell web server PoSH (http://poshserver.net/) for some administration reports. But i don't know how to format ouput.
From the start: i start the console with the default shortcut, with admin rights. I type Import-Module PoSHServer, then Start-PoSHServer. The web server starts, then i create a simple index.ps1 file, with just one line of code in the body section: $(command).
For example, i want to use the Get-Service Mpssvc command, but what i obtain is :
System.ServiceProcess.ServiceController
I try Get-Service MpsSvc | Select Name,Status. Output:
#{Name=MpsSvc; Status=Running}
Same thing for cmdlets Get-Process, i have an output with list of processes but it appears like this: System.Diagnostics.Process (AcroRd32) ...
However, some cmlets just like the Get-Date (used in the Posh demonstration web page) works fine and have a "normal" output.
I read the documentation, but there is no example which can help me for that.
How can i write powershell code to obtain a "clean" and console-like output?

I just downloaded and installed Posh-Server yesterday after reading this post.
If you want output to look like console inside a web-page you are probably looking at this from the wrong angle, you need to think string not console. Your code is supposed to be running inside of a here-string, in the example. So I got the hint here that the standard console formatter does not apply, posh-server will use whatever it wants to to turn your returned object into a STRING!. Your code output will get turned into a string using whatever formatter it deems applies unless you explicitly return a string - which the example script does correctly do. So try this on the console
get-process "power*" | out-string -width 80
And then try it in your posh-server script.
You probably really wanted this:
Get-Service MpsSvc | Select Name,Status | out-string -width 120
Hope that helps - I think the lack of examples in this project is a good thing because this is really a very simplistic web-server; lots of conceptual thinking required before you even start :) .

Related

Running help <command> and piping output to Where-Object or Select-Object returns empty rows

Running the command help firewall | Select-Object Category. The result is one column blank Category.
The strange thing is that the empty rows number represent the amount of rows that help firewall would result without calling piping it to Select-Object
Or I'm trying to filter the output of help firewall to return only rows with Name that starts with "Get". Running help firewall | Where-Object Name -like "Get" just returns nothing.
Why aren't these pipes on help working? They are working perfectly on other commands.
Powershell Version 5.1 and using default windows console.
To complement Zilog80's helpful answer with background information:
Get-Command help reveals that help is not a mere alias of the Get-Help cmdlet, but a (built-in) function (submit $function:help to see its definition).
As you've noticed yourself:
while Get-Help outputs an object ([pscsustomobject]) with properties that reflect help-topic metadata such as Category, which is then rendered as display text by PowerShell's output-formatting system,
the help function returns strings - a stream of text lines representing the rendered help topic - of necessity.
You can observe the difference in output type by piping to the Get-Member cmdlet (help firewall | Get-Member vs. Get-Help firewall | Get-Member)
The purpose of the help function is to wrap Get-Help with interactive paging, to allow convenient navigation through lengthy help topics that don't fit onto a single console (terminal) screen.
This paging is provided via an external program (by default, more.com on Windows, and less on Unix-like platforms, configurable via $env:PAGER, but only in PowerShell (Core) 7+), and since PowerShell only "speaks text" when communicating with external programs, help must send a stream of strings (lines for display) to them, which it does via
Out-String -Stream.
Note:
When the external paging programs find that their stdout stream isn't connected to a console (terminal), they take no action other than simply passing the input through (in Unix terms, they then behave like cat).
Hypothetically, the help function itself could determine this condition and then (a) not pipe to the paging program and (b) relay the object output by Get-Help as-is.[1] However, determining a command's output target from inside that command, using PowerShell code, may not even be possible.
[1] The function actually already takes this action when a custom pager defined via $env:PAGER is found to be a PowerShell command rather than an external program.
Check the feedback from help help in PowerShell :
You can also type `help` or `man`, which displays one screen of text at a
time. Or, ` -?`, that is identical to `Get-Help`, but only
works for cmdlets.
The helpcommand display "screen of text" which means it is outputting [System.String] objects, not [PSCustomObject] objects.
Only -? will behave identically to Get-help and will provide [PSCustomObject] objects.
To see what's going on, check the different output from :
help firewall | %{ $_.GetType() }
And
Get-help firewall | %{ $_.GetType() }
And, for cmdlet,
Select-Object -? | %{ $_.gettype() }

Assemble oneliners from multiline PS script

Need to manipulate ICS from command line batch script, but did not found suitable console tool. But found powershell script which do such task. The problem is the script is multiline and as I have zero experiences with PS, looking for someone who could create few oneliners from such script to easily run trhough batch script and parse its output. Need following oneliners:
List adapters with ICS enabled
Enable ICS for specific interfaces
Disable ICS on specific interface
Anyone could help me with this?
Why on Earth would they need to be one-liners? None of these are suitable for one-liners, for instance your first one could look like this.
(New-Object -ComObject HNetCfg.HNetShare -OutVariable netshare).EnumEveryConnection | foreach {[PSCustomObject]#{Interface = $netshare.NetConnectionProps.Invoke($_).name; ICSEnabled = $netshare.INetSharingConfigurationForINetConnection.invoke($_).sharingenabled}}

How to output specific information from a powershell command?

So I've been playing around with Powershell recently, trying some things with a basic command net user $username. As an example net user administrator produces an output that you can see at the bottom of this page.
My question is: How do I output specific elements of this?
I'm aware of pipes and have been trying to use them but I think I'm missing something as it never comes out right. Lets say, for example, I just want user name, full name, password expires and last logon to be shown as an output. What command do I use after the pipe to get this?
Many thanks!
net.exe is not a PowerShell cmdlet. Therefore getting information out it is processing the output of the executable as a string.
For example, retrieving the user name:
$netoutput = net user administrator
#User name is on the first line, separated by spaces, the actual username is last word
$netoutput[1].Split(" ")[-1]
If you are using Win10 1607 or newer, you could retrieve this information with the Get-LocalUser cmdlet
Get-LocalUser administrator | Select-Object Name,FullName,PasswordExpires,LastLogon

powershell get PID for specific app running for calling user

We have an ERP application with restrictive licensing, which we access via RemoteApp. We need to only allow one instance per user. So, I've been experimenting with PowerShell to try to do this.
What the script has to do is check and see if "pxxi.exe" is running for the calling user.
My first attempt used WMI. It worked, but it turns out WMI is very very slow.
(gwmi win32_process -Filter "name='pxxi.exe'" | ? {$_.getowner().user
-eq $env:username}).pid
My second attempt used Get-Process. This works great, but unfortunately requires elevated rights.
Get-Process pxxi -IncludeUserName | ? {$_.username -match $env:username}).id
My third attempt focused on the win32 command line program Tasklist.
$result = Invoke-Command { c:\windows\system32\tasklist.exe /fi
"username eq $env:username" /fi "imagename eq pxxi.exe"}
This works! And thanks to EBGreen's code, I can extract just the PID.
($result[3] -split '\s+')[1]
And while that runs really quick as an Administrator, for regular users, Tasklist runs as slow as WMI, above...
So, that puts me back to square one.
Does anyone out there know of a bit of code to quickly check and see if a user XYZ is running a process with name ABC.EXE ?? This needs to work for regular users (but regular users don't need to see other user's processes) and it needs to not take 30+ seconds to run.
I'm not married to powershell. VBScript would be fine. Or any little tool available on the internet.
Thanks!
For the example that you have:
($result[3] -split '\s+')[1]
Be aware that this works for just one result being returned. If you expect more than one instance then you should write a loop to iterate from the 4th item in the array on splitting each item to get that process's PID.
I gave up trying to find a way to do it in Powershell. Either the method was too slow, or required admin.
So I wrote something in C#:
c# - check if program is running for current user

Forcing PowerShell errors output in English on localized systems

I need to run some PowerShell scripts across various operating systems. Most of them are in English version, however, some are localized for example German, French, Spanish, etc. The problem is local system administrators mostly do not now PowerShell and in the case the script fails and throws an error at them, instead of reading it they just send screenshots of such error messages to me and if the cause to this error is not obvious I am stuck with typing it to g. translate to find out what is going on.
Is there a switch I can run the whole script or single command with or a parameter or any other way to force errors in PowerShell to be displayed in English instead of the language that is default for that particular machine?
You can change the pipeline thread's CurrrentUICulture like so:
[Threading.Thread]::CurrentThread.CurrentUICulture = 'fr-FR'; Get-Help Get-Process
I'm on an English system but before I executed the line above, I updated help like so:
Update-Help -UICulture fr-FR
With that, the Get-Help call above gave me French help on my English system. Note: if I put the call to Get-Help on a new line, it doesn't work. Confirmed that PowerShell resets the CurrentUICulture before the start of each pipeline which is why it works when the commands are in the same pipeline.
In your case, you would need to have folks install English help using:
Update-Help -UICulture en-US
And then execute your script like so:
[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'; .\myscript.ps1
[Threading.Thread]::CurrentThread.CurrentUICulture only affects to current one-liner, so you can use it for execution of single .ps1 file.
If you want to change messages to English throughout every command in a PowerShell window, you have to change the culture setting cached in PowerShell runtime with reflection like this:
# example: Set-PowerShellUICulture -Name "en-US"
function Set-PowerShellUICulture {
param([Parameter(Mandatory=$true)]
[string]$Name)
process {
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture($Name)
$assembly = [System.Reflection.Assembly]::Load("System.Management.Automation")
$type = $assembly.GetType("Microsoft.PowerShell.NativeCultureResolver")
$field = $type.GetField("m_uiCulture", [Reflection.BindingFlags]::NonPublic -bor [Reflection.BindingFlags]::Static)
$field.SetValue($null, $culture)
}
}
(from https://gist.github.com/sunnyone/7486486)