Powershell

Customizing your Powershell profile

Everytime when open a powershell, I always change the directory where my scripts reside which is C:\scripts folder. There is an easy way to accomplish this. We can save all these customization in to our profile so that when we open a session, they will get applied automatically. Some of these are below as examples;

dir $profile

(check whether it’s already been created if not create one

new-item $profile -Force

Then start putting your changes in to your profile

Add-Content -Value ‘cd c:\Scripts’ -Path $profile

Add-Content -Value ‘$host.privatedata.errorforegroundcolor=”green”‘ -Path $profile

Add-Content -Value ‘write-host “Hello Murat – Nice to see you again!” -foregroundcolor magenta’ -Path $profile

You can do a lot more but these are just a few examples..

Let’s have a looks….

profile

Advertisements
Containers, Docker, Hyper-V, Nano Server, Powershell, Server 2016

More about Containers …

I have found this article in GitHub site really nice and easy to understand. I think more and more people need to understand the terminology first and it is crucial. Containers will be compared to Virtual Machines but there are lots of differences…


  • Containers are about Software!
  • Traditionally we use the following process to run software:
    • Find the software, usually a standalone web site.
    • Download the software, usually a zip file or some sort of installer.
    • Then we install the software, often extracting a zip file or running an installer.
    • Then we run the installed software.
  • You can learn alot about containers by relating them to the process above. Here’s what it looks like to run software with containers:
    • Find the software, on Docker Hub.
    • Download the software with docker pull, comes down as an image which is much like a zip file or msi installer. An image is an application packaging format.
    • Instead of installing the software, we create a container. So, a container–a stopped container–is like installed software. Docker unpacks the image onto the computer, creating a container. Note: if you just want to create a container, you can use docker create.
    • Then we run the container which is exactly like running an exe. It’s the same thing under the covers!!!
    • We often use docker run to orchestrate all of these steps with one command, how convenient!
  • docker exec can be thought of as running another copy of our installed software, like when we launch an executable twice. For example, two copies of Microsoft Word. Or with MongoDB, we might run two mongo clients. After a container is created and running, we can use docker exec to run multiple applications, or multiple copies of the same app, inside the container.

 

Powershell, Server 2016

Set-WebApplicationProxyApplication

The Set-WebApplicationProxyApplication cmdlet modifies settings of a web application published through Web Application Proxy. Specify the web application to modify by using its ID. Note that the method of preauthentication cannot be changed. The cmdlet ensures that no other applications are already configured to use any specified ExternalURL or BackendServerURL.

Set-WebApplicationProxyApplication -ID 994A4543-7983-77A3-1E6D-1163E7419AC1 -ExternalUrl https://webapp.abc.com/

Set-WebApplicationProxyApplication
[-ClientCertificateAuthenticationBindingMode <String>]
[-BackendServerCertificateValidation <String>]
[-ExternalUrl <String>]
[-ExternalCertificateThumbprint <String>]
[-BackendServerUrl <String>]
[-DisableTranslateUrlInRequestHeaders]
[-EnableHTTPRedirect]
[-ADFSUserCertificateStore <String>]
[-DisableHttpOnlyCookieProtection]
[-PersistentAccessCookieExpirationTimeSec <UInt32>]
[-EnableSignOut]
[-BackendServerAuthenticationMode <String>]
[-DisableTranslateUrlInResponseHeaders]
[-BackendServerAuthenticationSPN <String>]
[-Name <String>]
[-UseOAuthAuthentication]
[-InactiveTransactionsTimeoutSec <UInt32>]
[-ClientCertificatePreauthenticationThumbprint <String>]
[-ID] <Guid>
[-CimSession <CimSession[]>]
[-ThrottleLimit <Int32>]
[-AsJob]
[<CommonParameters>]

Powershell, Server 2016

How to access a Nano Server

Because Nano Server does not support a local session, it must be accessed remotely.

IP Address of the Nano Server: 10.10.10.1
1. Start an elevated PowerShell ISE session.
2. Set the Trusted Host. This is a one-time setting for each remote machine. You’re basically telling your development machine to trust the remote Nano Server.
Set-Item WSMan:\LocalHost\Client\TrustedHosts “10.10.10.1”
3. Start the session.
Enter the following commands into the PowerShell ISE command line:
$ip = “10.10.10.1” 
$s = New-PSSession -ComputerName $ip -Credential ~\Administrator
Enter-PSSession -Session $s

Powershell, Server 2016

Deploy Network Controller using Windows PowerShell

Install-WindowsFeature -Name NetworkController –IncludeManagementTools

Restart-Computer

New-NetworkControllerNodeObject –Name <string> -Server <String> -FaultDomain <string>-RestInterface <string> [-NodeCertificate <X509Certificate2>]

Install-NetworkControllerCluster –Node <NetworkControllerNode[]> –ClusterAuthentication <ClusterAuthentication> [-ManagementSecurityGroup <string>][-DiagnosticLogLocation <string>][-LogLocationCredential <PSCredential>] [-CredentialEncryptionCertificate <X509Certificate2>][-Credential <PSCredential>][-CertificateThumbprint <String> ] [-UseSSL][-ComputerName <string>]

Install-NetworkController –Node <NetworkControllerNode[]> –ClientAuthentication <ClientAuthentication> [-ClientCertificateThumbprint <string[]>] [-ClientSecurityGroup <string>] -ServerCertificate <X509Certificate2> [-RESTIPAddress <String>] [-RESTName <String>] [-Credential <PSCredential>][-CertificateThumbprint <String> ] [-UseSSL]

Powershell, SCCM, System Center Configuration Manager

Hot to use PowerShell to manage System Center Configuration Manager

The easiest way is by launching the Configuration Manager console. In the upper left corner, there’s a blue rectangle. Click the white arrow in the blue rectangle, and choose Connect via Windows PowerShell.

cm

Now, you need to import the Configuration Manager module by using the Windows PowerShell Import-Module cmdlet. To import the Configuration Manager module, you will have to specify the path to the Configuration Manager module or change to the directory that contains the module. Here, we’re going to change to the module’s directory.

PS C:\>

PS C:\> CD ‘C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager’

PS C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager>

Then import the module;

Import-Module .\ConfigurationManager.psd1

To run the Configuration Manager cmdlets, you need to switch the path to the Configuration Manager site.

PS C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager>CD MSN

MSN – is my site code

PS MSN:\>

Confirm that the Configuration Manager module has been loaded by using the cmdlet below;

PS MSN:\> Get-CMSite

BuildNumber : 7958

Features : 0000000000000000000000000000000000000000000000000000000000000000 InstallDir : C:\Program Files\Microsoft Configuration Manager

Mode : 0

ReportingSiteCode :

RequestedStatus : 110

ServerName : cm1.company.pri

SiteCode : MSN

SiteName : MSN pri site

Status : 1

TimeZoneInfo : 000001E0 0000 000B 0000 0001 0002 0000 0000 0000 00000000 0000 0003 0000 0002 0002 0000 0000 0000 

Type : 2

Version : 5.00.7958.1000

Powershell

Powershell special characters

When you are using Powershell, it really helps if you use special characters to write your own scripts or interpret someone else’s scripts. Some of them are really common and get used very often….

# Hash – Single line comment

 

#This script is for ....
#This variable is to ...

 

$ Dollar sign – is used to declare a variable

$ComputerName = "Server1"
$password = "p@sw0rd"

 

| Pipeline- Executes the left side and with  the output feeds to the right

 

Get-Process | Select -first 10

 

% Percentage – Short for “FOREACH”

 

% ($Server in $Servers) { Write-Host $_}

 

? Question Mark – Short for “Where”

 

Get-process | ? {$_.name -like 'win*' -and $_.status -eq 'Running'}

 

@ () – Declares an array

 

$Servers = @ ("server1", "server2", "server3")

 

@ {} – Declares an hash table

 

$servers = @{"server1" = "Dell";
             "server2" = "HP";
             "server3" = "Nutanix"}

$servers["server3"]
Nutanix

------------------------------------ OR

$params = @{};
$params['class'] = 'Win32_DiskDrive';
$params['filter'] = 'size=256052966400'; #find a drive which is 256GB in size
		  
Get-WmiObject @params
Get-WmiObject -Class 'Win32_DiskDrive' -Filter 'size=256052966400'

 

& Ampersand – Executes strings as commandlets

 

& "Get-Scheduledjob"

 

! Exclamation – Short for “not”

$serverName = $null;
if(!$serverName) { Write-Host '$a is null' }

 

:: Double colon – Reference static member of a class. The class name must be enclosed in square brackets.

[string]::Equals = ("Computers", "COMPUTERS")
False # this will compare these two strings and returns false