Last active
February 23, 2019 17:59
-
-
Save SteveL-MSFT/82f084b17abae4536412f0f286b474d3 to your computer and use it in GitHub Desktop.
Demos of writing OS agnostic PowerShell Core scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PowerShell remoting over SSH | |
## Configuring Windows | |
### Install SSHD as Feature on Demand | |
### Modify sshd_config for powershell subsystem | |
## SSH remoting | |
ssh –l user@domain computer | |
pwsh | |
## PowerShell remoting | |
Enter-pssession -hostname computer -username user@domain | |
Enter-pssession -hostname user@computer | |
# PowerShell remoting over WSMan | |
## Office365 from non-Windows | |
#### With macOS Mojave use OpenSSL from MacPorts not HomeBrew | |
$cred = get-credential | |
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -AllowRedirection -Authentication Basic | |
Import-pssession $s | |
Get-mailbox | |
## Windows from non-Windows | |
### Setup HTTPS endpoint for WinRM | |
Set-Item WSMan:\localhost\Service\Auth\Basic $true | |
Import-WinModule pki | |
$selfSignedCert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName $env:COMPUTERNAME | |
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $selfSignedCert.Thumbprint | |
New-NetFirewallRule -DisplayName "Windows Remote Management (HTTPS-In)" -Name "Windows Remote Management (HTTPS-In)" -Profile Any -LocalPort 5986 -Protocol TCP | |
### WSMan remoting into Windows from non-Windows | |
$cred = Get-Credential steve | |
### Use a real cert! But if using self-signed for testing: | |
$so = [System.Management.Automation.Remoting.PSSessionOption]::new() | |
$so.SkipCACheck = $true | |
$so.SkipCNCheck = $true | |
Enter-PSSession -ComputerName slee-office -Credential $cred -Authentication Basic -UseSSL -SessionOption $so | |
## Docker | |
### Ubuntu 16.04 | |
docker run -it mcr.microsoft.com/powershell:latest | |
$psversiontable | |
cat /etc/lsb-release | |
### Alpine | |
docker run -it mcr.microsoft.com/powershell:alpine-3.8 | |
cat /etc/alpine-release | |
# Handling OS differences | |
## Detecting the OS | |
$IsWindows, $IsLinux, $IsMacOS | |
if ($IsMacOS) { "Using macOS" } | |
## Detecting Windows for both Windows PowerShell and PSCore6 | |
if ($null -eq $IsWindows –or $IsWindows –eq $true) { “Windows!” } | |
# Case Sensitivity Filesystem and Environment Variables | |
## Modules - Linux | |
mkdir TestModule | |
New-ModuleManifest -Path ./TestModule/testmodule.psd1 | |
Import-Module ./TestModule | |
mv ./TestModule/testmodule.psd1 ./TestModule/TestModule.psd1 | |
Import-Module ./TestModule | |
## Casesensitivity - Linux | |
$env:path | |
$env:PATH | |
## Path separator | |
$env:PSModulePath | |
$psmodulepath = $env:PSModulePath -split [System.IO.Path]::PathSeparator | |
$psmodulepath += "CustomModulePath" | |
$psmodulepath | |
$psmodulepath -join [System.IO.Path]::PathSeparator | |
## $env:TEMP vs $env:TMP vs $env:TMPDIR | |
[System.IO.Path]::GetTempPath() | |
## Console output improvements | |
### Easier VT100 escape codes | |
"`e[5m`e[35mHello `e[1m`e[36mWorld!`e[m" | |
### Easier Unicode | |
"PowerShell `e[31m`u{2665}`e[m community `u{1f44d}, Open Source `u{1F44C}, and Linux `u{1F427}!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment