Assemble oneliners from multiline PS script - powershell

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}}

Related

How can I use a powershell multi-line command as a single line in a command prompt window

Is there a way to enter at once in the command prompt window?
This code works well without errors when you enter it line by line in the poweshell window.
c:\Windows\system32>powershell -command $ACL=New-Object System.Security.AccessControl.FileSecurity ^
$AccessRule=New-Object System.Security.AccessControl.FileSystemAuditRule("User", "Write", "Success") ^
$ACL.AddAuditRule($AccessRule) ^
$ACL | Set-Acl C:\Users\User\Desktop\testfile.txt
I searched a lot of ways but I couldn't make it in one line.
I tried many different ways, but they all failed.
I want to use this method in the batch-file.
I really need the help of the power users.
Looking forward to your best response.
Thank you.
There are two things here. First - you need the ; character to separate commands.
Second - look after double quotes and single quotes :)
How to run PS commands with the CLI: https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/About/about_PowerShell_exe?view=powershell-5.1
So - it needs to look like this:
C:\Temp>powershell "$ACL=New-Object System.Security.AccessControl.FileSecurity; $AccessRule=New-Object System.Security.AccessControl.FileSystemAuditRule('User', 'Write', 'Success');$ACL.AddAuditRule($AccessRule);$ACL | Set-Acl C:\Users\User\Desktop\testfile.txt"

Get output when using .run

I'm trying to run a program that will web scrape from Pastebin using PowerShell. I used the following code to do so:
Set Wshell = CreateObject("WScript.Shell")
Wshell.Run "%ComSpec% /c powershell & $result = Invoke-WebRequest
""https://pastebin.com/raw/wAhYB4UY"" & $result.content ", 0, True
$result.content will bring up everything I need from Pastebin. How can I transfer $result.content to a VBScript variable?
I know this is possible using the Exec() method as demonstrated here, but I can't use it because I want my code to stay hidden, which to my knowledge is not possible with Exec() (without having a window popping and closing)
I also don't want to use File I/O in Powershell because that can really complicate other things I want my program to do in the future; however, If absolutely no options are available, then I can use it.
EDIT: Some readers pointed out that my script only consists of running Powershell, so why not program my script in PowerShell? Well, not everything I am planning for this script to do can be done in PS. for example, I want my script to type some stuff outside of PS. I also want to wait until the user has pressed a certain key, in my case PrtSc (which will create a popup a message using MsgBox).
$Path = 'https://pastebin.com/raw/etc'
$Raw = Invoke-WebRequest $Path
$Raw.Content
What do you want to do with the data that using VBScript is the preferable tool?

preplog.exe ran in foreach log file

I have a folder with x amount of web log files and I need to prep them for bulk import to SQL
for that I have to run preplog.exe into each one of them.
I want to create a Power script to do this for me, the problem that I'm having is that preplog.exe has to be run in CMD and I need to enter the input path and the output path.
For Example:
D:>preplog c:\blah.log > out.log
I've been playing with Foreach but I haven't have any luck.
Any pointers will be much appreciated
I would guess...
Get-ChildItem "C:\Folder\MyLogFiles" | Foreach-Object { preplog $_.FullName | Out-File "preplog.log" -Append }
FYI it is good practice on this site to post your not working code so at least we have some context. Here I assume you're logging to the current directory into one file.
Additionally you've said you need to run in CMD but you've tagged PowerShell - it pays to be specific. I've assumed PowerShell because it's a LOT easier to script.
I've also had to assume that the folder contains ONLY your log files, otherwise you will need to include a Where statement to filter the items.
In short I've made a lot of assumptions that means this may not be an accurate answer, so keep all this in mind for your next question =)

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

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 :) .

How to read ATOM XML in PowerShell

How to call System.ServiceModel.Syndication.SyndicationFeed from PowerShell?
I was trying to read RSS feed from Powershell, and need to traverse through feed, I am wondering what would be the way to do it?
I could not find any example on it.
Thanks in advance!!
Ramani
What you're really trying to do is parse XML with PowerShell. This one liner will get you the entries from this question:
((New-Object Net.Webclient).DownloadString("http://stackoverflow.com/feeds/question/10589059") -as [xml]).feed.entry
It creates a new webclient, and downloads the URL, turns it into XML, and then just dots into the XML elements (as you can do in PowerShell).
It's a very simple one liner that packs in a lot of the power of PowerShell. A similar example is at the beginning of Bruce Payette's "PowerShell In Action"
Hope this Helps
Here's what I came up with in a few seconds:
$url = "http://stackoverflow.com/feeds/question/10589059"
[System.Reflection.Assembly]::LoadWithPartialName("System.ServiceModel") | Out-Null
[System.ServiceModel.Syndication.SyndicationFeed] $feed = [System.ServiceModel.Syndication.SyndicationFeed]::Load([System.Xml.XmlReader]::Create($url))
$feed | Get-Member