Run PowerShell Scripts from Task Scheduler
I assume that your are familiar with the Task Scheduler in Windows, so this one should be a quick one:
- Create a new task
- Create an Action
-
Program/Script: <pre class="lang:default decode:true ">powershell.exe</pre>
-
Add arguments: <pre class="lang:default decode:true ">-NoProfile -ExecutionPolicy Bypass C:\path_to_your_script.ps1</pre>
- Set all the other needed things liker any other scheduled task
- Have an automated powershell script, easy right?
htop on CentOS 7
Studying for the LFCS, I found a handy addition to top, called htop.
The problem is the htop software package seems not to be part of the default yum repository. But the fix is easy, first install epel (Extra Packages for Enterprise Linux) and THEN you can install htop.
# 1. Install epel yum -y install epel-release
# 2. Install htop yum -y install htop
Done.
CentOS Add alias
Now that I scheduled my LFCS exam I am trying to spend more time with Linux. A big pet peeve of mine is that CLS and CLEAR are not the same thing, one works in CMD one works in the CLI but you can’t interchange them between Linux and Windows.
( Yet PowerShell is happy with either…!)
So to make life easier here how to add an alias in CentOS so both clear and CLS will work:
Aliases are defined in the .bashrc file on root. Can’t see it? It’s a hidden file so ‘ls -al’ should do the trick. See it now?
We can edit it just with vi:
vi .bashrc
The line we want to append should look like so:
alias keyword=’target’
So in my case: alias cls=’clear’
Keyword is the word you’d enter and target is the word / command that will actually be entered in the shell/CLI.
You’ll need to restart the shell or log out and back in again for it to work. Also make sure to save your .bashrc file for it to work….don’t ask… :oP
My $profile
In my previous post I talked about Powershell profiles, that is my current profile:
$br = "`r" clear if ($host.name -eq 'ConsoleHost') { $Shell=$Host.UI.RawUI $size=$Shell.BufferSize $size.width=120 $size.height=3000 $Shell.BufferSize=$size $size=$Shell.WindowSize $size.width=120 $size.height=30 $Shell.WindowSize=$size $Shell.BackgroundColor="Black" $Shell.ForegroundColor="White" $Shell.CursorSize=10 $Shell.WindowTitle="Console PowerShell" } #PART 2 function Get-Uptime { $os = Get-WmiObject win32_operatingsystem $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime)) $Display = "" + $Uptime.Days + " days / " + $Uptime.Hours + " hours / " + $Uptime.Minutes + " minutes" Write-Output $Display } function Get-Time {return $(Get-Date | ForEach {$_.ToLongTimeString()})} function prompt { Write-Host "[" -noNewLine Write-Host $(Get-Time) -ForegroundColor Green -noNewLine Write-Host "] " -noNewLine Write-Host $($(Get-Location).Path.replace($home,"~")) -ForegroundColor cyan -noNewLine Write-Host $(if ($nestedpromptlevel -ge 1) { '>>' }) -noNewLine return "> " } #PART 3 Set-Location C:\ $MaximumHistoryCount=1024 $IPAddress=@(Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIpGateway})[0].IPAddress[0] $IPGateway=@(Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DefaultIpGateway})[0].DefaultIPGateway[0] $PSExecPolicy=Get-ExecutionPolicy $PSVersion=$PSVersionTable.PSVersion.Major #PART 4 Write-Host "______________________________________________________________________________________________________________" -ForegroundColor Green $br Write-Host "|`tComputerName:`t`t" -nonewline -ForegroundColor Green;Write-Host $($env:COMPUTERNAME)"`t`t`t`t`t" -nonewline -ForegroundColor white;Write-Host "UserName:`t" -nonewline -ForegroundColor Green;Write-Host $env:UserDomain\$env:UserName"`t`t" -nonewline -ForegroundColor white $br Write-Host "|`tLogon Server:`t`t" -nonewline -ForegroundColor Green;Write-Host $($env:LOGONSERVER)"`t`t`t`t" -nonewline -ForegroundColor white;Write-Host "IP Address:`t" -nonewline -ForegroundColor Green;Write-Host $IPAddress"`t`t" -nonewline -ForegroundColor white $br Write-Host "|`tPS Execution Policy:`t" -nonewline -ForegroundColor Green;Write-Host $($PSExecPolicy)"`t`t`t" -nonewline -ForegroundColor white;Write-Host "`tPS Version:`t`t" -nonewline -ForegroundColor Green;Write-Host $PSVersion"`t`t`t" -nonewline -ForegroundColor white Write-Host "|`tUptime:`t`t`t" -nonewline -ForegroundColor Green;Write-Host $(Get-Uptime)"`t`t`t`t`t`t" -nonewline -ForegroundColor white $br Write-Host "______________________________________________________________________________________________________________" -ForegroundColor Green $br
PowerShell Profiles
If you work with Powershell, you most likely end up doing things multiple times and even eventually have the Console look the way YOU want and load modules and Ps-snapins as you need to. The best way to do that using Powershell profiles.
Profiles, especially starting out, can be a little bit confusing though, as there are actually 6 profiles.
[table id=3 /]
The most common one to use is most likely the “Current User, Current Host – ISE” or the “Current User, Current Host – console” profile.
The ‘$profile’ variable points, depending if using the ISE or the console to the corresponding powershell script that has the profile information in it.
# From Console [1:52:36 PM] C:\> $profile C:\Users\awittig\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
# From ISE PS C:\> $profile C:\Users\awittig\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
You can also get the hidden properties by using the -Force command:
[1:53:31 PM] C:\> $PROFILE | fl * -Force AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1 CurrentUserAllHosts : C:\Users\awittig\Documents\WindowsPowerShell\profile.ps1 CurrentUserCurrentHost : C:\Users\awittig\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 Length : 75
To note, by default on a brand new computer there is no $profile there, so you have to create it on your own!
You can test if you have a powershell profile-file setup by running this command:
Test-Path $profile
The output should be a Boolean, either $true or $false. See below:
As you can see, I do not have a profile file for the ‘CurrentUserAllHosts’ parameter.
To create a new $profile.CurrentUserAllHosts profile we can do this:
new-item $PROFILE.CurrentUserAllHosts -ItemType file -Force
There are several ways to edit the file that you just created:
- You can just navigate to the $profile.CurrentUserAllHosts file and right click on it
- You can open it for editing in the ISE: <pre class="lang:ps decode:true">ise $profile.CurrentUserAllHosts
</pre>
- You can open it with notepad / explorer <pre class="lang:ps decode:true ">explorer $profile.CurrentUserAllHosts</pre>
4. My favorite way, you can edit it with VIM
<pre class="lang:default decode:true ">vim $profile.CurrentUserAllHosts</pre>
(See my previous blog-post how to use vim for inline editing)</li> </ol>