Decode powershell command to code - powershell

It is possible decode (or show what to do) command Powershell?
I try use command connect-msolservice, but i get exceptions:
.
So maybe if I get content command, i can configure system to this connection.

Yes, you can use ILSpy to decode powershell dll. Download ILSpy.
For find path .dll with your cmdlets, use powershell command:
Get-Command connect-msolservice | fl DLL,ImplementingType

Or you can use a native solution to view the Metadata of the builtin (or any other cmdlets)
$Metadata = New-Object System.Management.Automation.CommandMetaData (Get-Command Connect-MSOLService)
$Contents = [System.Management.Automation.ProxyCommand]::Create($Metadata)
credit to http://windowsitpro.com/blog/powershell-proxy-functions

Related

wlanapi.dll in powershell. Disable background scanning

Looking for some help with a script. I have tried and failed. I am not really advanced in powershell.
importing dlls is new for me. Any help is appreciated.
I want to use powershell to import the wlanapi.dll and use micrsoft native wifi functions to disable wireless background scanning, and enfore streaming mode.
The script should do this on execute. That way I can run it, or set it in a start up script.
https://learn.microsoft.com/en-us/windows/win32/api/wlanapi/nf-wlanapi-wlansetinterface?redirectedfrom=MSDN
Functions I am wanting to use:
wlan_intf_opcode_background_scan_enabled
wlan_intf_opcode_media_streaming_mode
Import is something you do via the PSModule paths where your modules or DLLs live.
You must tell PowerShell where the DLL is, no different than you'd have to if you loaded a module (.psm1 file with or without a manifest) of which you did not install to one of the defined PowerShell module paths.
You can use Add-Type...
Add-Type -Path $UncToCustomDll
... yet also, you can also use reflection:
$customDLL = 'UncToYourDLL'
See also Lee Holmes article on the topic here:
Load a Custom DLL from PowerShell
If you try to import and it's not in a know location, you get this.
Import-Module SomeNewCustomOr3rdP.dll
Import-Module : The specified module 'SomeNewCustomOr3rdP.dll' was not loaded because no valid module file was found in any module directory.
Of course, that error is pretty specific. It has no idea where to find it because that name does not match a module name.
So, this ...
Import-Module 'c:\users\mj\desktop\SomeNewCustomOr3rdP.dll'
Or create a folder of the same basename as the DLL in the PSModulePath, copy the DLL to the that named folder and use import as normal
C:\Users\<username>\Documents\WindowsPowerShell\Modules\SomeNewCustomOr3rdP\SomeNewCustomOr3rdP.dll'
Then this...
Import-Module SomeNewCustomOr3rdP
... should work as expected. All-in-all, Add-Type, Import-Module, and Reflection.Assembly::LoadFile($customDll), all accomplish the same thing, grant you access to the resource you specified.
If you are using a 3rdP DLL/Module, all this has to be done manually. If you are using published modules/packages that are in the MS powershellgallery.com, then this:
# Find all modules with wlan in the name
Find-Module -Name '*wlan*' |
Format-Table -AutoSize
# find all packages with wlan in the name
Find-Package -Name '*wlan*' |
Format-Table -AutoSize
# Get the detail on wlanapi specifically
Find-Package -Name 'wlanapi'
# Download and save a module or package
Find-Package -Name 'wlanapi' |
Save-Package -Path "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
Install-Package -Name 'wlanapi' -Force
Import-Module -Name wlanapi

get receiving bytes and sending bytes using netsh command in powershell

NetAdapter module is not installed. I have to get the traffic flow (receiving Bytes and sender Bytes) like Get-NetAdapterStatistics commmand using netsh command. is it possible? kindly help
i am not an admin to install new modules.
Thanks
Per the comments, this is not a great question (you really should attempt to write some code first) but I have some suggestions anyway. You can actually install modules without being an admin, just use:
Install-Module <modulename> -Scope CurrentUser
And it will go in to the Documents\WindowsPowerShell\Modules directory under your profile.
However if you want to try to get these values into a PowerShell object without having that cmdlet, you could use the old DOS command netstat /e and scrape the text result to turn it in to an object:
$NetStat = (& netstat /e) -split '\s+'
$Stats = New-Object -TypeName PSObject -Property #{
Sent = $NetStat[9]
Received = $NetStat[8]
}
$Stats
Returns:
Sent Received
---- --------
256063580 1179546715

Validate "path"

The following code works to run powercfg.exe, but I would like to make it more flexible and user friendly by testing for powercfg.exe and exiting gracefully with a logged error when the user provides a bad $executable. It seems that the file is being found in some part of %path%, but Test-Path is not resolving the path the same way Start-Process does. Is there some mechanism to expand the path automatically, or do i need to actually use the Path env var to do this manually?
$resource = 'C:\rtc.pow'
$executable = "powercfg.exe"
$argumentList = "-import $resource d03b6c96-607f-412c-b47b-417fa8d391af"
Start-Process -FilePath:$executable -argumentList:$argumentList
You can use get-command powercfg.exe and get its Path attribute to receive full name of the executable.
PS C:\Users\me> get-command powercfg.exe | select -expand path
C:\Windows\system32\powercfg.exe

How can i take a user dump using powershell

I want to take user dump of a process using powershell How can i do it?
The same I get on rightclicking the process in Taskmanager
Based on this article (archived) from Risksense.
MiniDump function from native comsvcs.dll Windows dll could be used.
Like:
Powershell -c rundll32.exe C:\Windows\System32\comsvcs.dll, MiniDump {ID-of-the-process} $Env:TEMP\my_dump_file.bin full
The easiest way is to use Procdump from Sysinternals toolkit. Use Get-Process to get process id, which you can pass to Procdump for actual dumping.
Edit:
I'd still rather use readily available tools instead of the hard way. Have you got a valid business reason? Since you insist, there is a Win32 API call that creates user mode memory dumps. It can be invoked from .Net code, so either use P/Invoke or embed C# into your Powershell code. This is left as an exercise to the reader.
Hi sorry I'm not much help. I've never used a DUP file before. But there is a WMI class called Win32_Process:
Get-WMIObject -Class Win32_Process
Not sure if that's the info you are looking for. Has different properties than Get-Process.
I had a similar use case where I needed to create a dump for an IIS process. Granted I could have used DebugDiag, but I ended up going down this path. Here's what I used (and works pretty well, I should add):
$procid = Get-Process | Where-Object {$_.ProcessName -eq 'w3wp'} | Select-Object ProcessName,Id
New-Item -Path "c:\temp\Dumps" -Type directory -Force
cmd.exe /c "c:\temp\procdump64.exe" $procid.id -accepteula -mp "c:\temp\Dumps"
Furthermore, you could use these dump files for analysis using DebugDiag too. So it's a win-win in my opinion.
PS: Theoretically, one could also get the Process ID using the Get-CimInstance cmdlet. So something like this would also work:
Get-CimInstance -Query "SELECT * from Win32_Process WHERE name LIKE 'w3wp%'"

webget in Powershell

Is there any command equivalent to webget in WindOS's PowerShell?
I am trying to create a script to download all publicly available files from the website. I am making the custom script because I need to store the files in specific directory structure (depending on name, type and size).
In PowerShell v2, use a WebClient:
(New-Object System.Net.WebClient).DownloadFile($url, $localFileName)
In v3, the Invoke-WebResquest cmdlet:
Invoke-WebRequest -Uri $url -OutFile $localFileName
Another option is with the Start-BitsTransfer cmdlet:
Start-BitsTransfer -Source $source -Destination $destination
In PowerShell V3, you can use the new cmdlet Invoke-WebRequest to send an http or https request to a web site/service e.g.:
$r = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+mile
However to specifically download a file it is probably easiest to use the .NET API WebClient.DownloadFile() e.g.:
$remoteUri = "http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png"
$fileName = "$pwd\logo.png"
$webClient = new-object System.Net.WebClient
$webClient.DownloadFile($remoteUri, $fileName)
you can use the .NET class WebClient to download files.
PS > $source = "http://www.unsite.fr/untruc.zip"
PS > $destination = "c:\temp\untruc.zip"
PS >
PS >$wc = New-Object System.Net.WebClient
PS >$wc.DownloadFile($source, $destination)
If you prefer a "native" PowerShell cmdlet that works in PowerShell V2 or V3, I recommend Get-HttpResource from the PowerShell Community Extensions (PSCX). While PSCX surprisingly does not have the API available online (you have to install the extensions then you can use the normal PowerShell help to explore each command), I managed to find the API for Get-HttpResource here. Using the cmdlet can be as simple as this:
$myPage = Get-HttpResource http://blogs.msdn.com/powershell
However, there are a variety of parameters to the cmdlet that let you specify media type, credentials, encoding, proxy, user agent, and more.