Skip to main content

Posts

Showing posts with the label PowerShell

Error: the type or namespace name does not exist in the namespace using visual studio 2017

When compiling an error will occur that a name space does not exist. An example error like the message below:
The type or namespace name 'Microsoft.Office.Interop.Outlook' does not exist in the namespace 'Microsoft'(are you missing an assembly reference?)
The system is trying to find a reference for a specific namespace or dll.
One solution for this is to add the reference to the missing namespace.
Find the “solution explorer” window like the image below:


After opening the “solution explorer” window, right click the name of the project just below the solution explorer. Beside the C# icon.
A new window will open like the image below. Click “Add”, then click “Reference”.



After clicking “Reference”, the “Reference Manager” window will open.
Click “COM” option, then use the filter option to go directly to the desired namespace or reference. Since the error is a Microsoft namespace, type “Microsoft” and “Microsoft” namespace will be filtered from the list. See image below…

PowerShell Insert Multiple Lines or text to another file dynamically

Copying and pasting text to another file is a common thing to do, copy and paste done. Boom!
In PowerShell scripting copying and pasting text from one file to another dynamically isn’t as easy as it is done in a spreadsheet or document. Dynamically while the code is running  and doing things in the background without human intervention.
As the odds say, if there’s a will there’s a way. And making things efficient and easy is a good way in life.
If ever a scenario occurs that you need to copy text or lines in a file from one file to another, without breaking the code execution. Script below works just fine. Just insert the code where it is needed.

PowerShell code snippet:
#================================
$reader=[System.IO.File]::OpenText("D:\test-read-paste\main1.txt") #get-content can also be used
$lineNumberx=3 #adjust this number where you need to start inserting the text
#Loop line by line and process while($null-ne ($line=$reader.ReadLine())) {
$textToAdd=$line
$newLineToAd…

PowerShell repeat spaces or characters

Example code of repeatingspaces within a string:
$xrepeat = ' ' * 11 $string_with_spaces = “ Hello World of Spaces!!!”
Write-output $xrepeat $string_with_spaces
Output image:

To indent or tab a string:
$Tab = [char]9 Write-Output "$Tab Hello Tab Indention!!!"
Output image:

To add other HTML character in PowerShell string:
$Euro_symbol = [char]8364 Write-Output "$Euro_symbol Hello Euro Symbol in PowerShell!!!"
Output image:


================================ Free Android Apps:
Click  links below to find out more:
Excel Keyboard guide: https://play.google.com/store/apps/details?id=chrisjoms.myexcelapplicationguide

Heaven's Dew Fall  Prayer app for Android :
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot.sg/2014/09/how-to-recite-rosary.html

Working with text in PowerShell

Working with a text file or text format in PowerShell is a good thing to learn.
Getting or monitoring output from PowerShell for further processing or analysis and having it in text file format can easily be imported to other applications.
Working with text or other format, requires these basic operations: Save, Overwrite, Append and Save As.
Save, overwrite, append and save as is a common operation when editing word documents, excel file and other office applications since it’s freely available on the menu of the application. But can we do this in PowerShell? The answer is a resounding, yes.


So, how to do it? Examples below shows how to work with text files in PowerShell and do the operation mentioned above.

Equivalent operations for Save, Overwrite, Append and Save As in PowerShell:
===Set-content ==== will overwrite the file ===Add-content ==== will just add or append something to the file ===Out-File ==== is like "Save As" since you have the option to save the file to a…

Change Password of Scheduled Tasks in PowerShell

How to change Tasks Scheduler password?
How to set a new password for scheduled tasks in Windows Task Scheduler using a script?
PowerShell is always a friend to cure laziness in an effective and efficient way.
Of course, not all things can be done by automation. Sometimes manual method is more applicable and a secure method on some scenario.
If you have a few servers that has scheduled tasks on it and need to change the password occasionally to comply with cyber security policies, changing tasks manually might be painful and chances are some tasks might be missed.
Changing the password in Task Scheduler using PowerShell is an efficient method if the task has to be done repeatedly on a few servers.
To change the password, the TaskName must be known since it’s an input or a required field for the command used to change the Task password.
To get Tasks Name use the PowerShell code below:
Get-ScheduleTask - TaskPath "\"| Get-ScheduledTaskInfo
The TaskPath refers to location of the Task Na…

PowerShell Ping IP get status or reply

How to ping sweep a network?
How to ping sweep a subnet?
There’s a lot of ways to ping the whole subnet or do a sweep ping to the whole network, of course download any free software that can do this, or just manually ping the whole network by manually changing the IP address.
Or do an old school method, using a batch file. This link shows how to ping the whole subnet via command line. http://quickbytesstuff.blogspot.sg/2015/11/how-to-ping-whole-subnet-using-batch.html
Since PowerShell is readily available from Windows 7 up to the newer versions of Windows. Then PowerShell is the perfect choice to do this task.
Open PowerShell ISE or the PowerShell command line, then type or copy the commands below:
$ping = New-Object System.Net.Networkinformation.Ping 1..254 | % { $ping.send(“192.168.1.$_”) | select address, status | ft -auto }

Change the IP Address (192.168.X.X) to the IP address range of your network.
What the script does, is to Ping the whole network starting from 1 to 254, basically the w…

Check PowerShell Version

How to check PowerShell version?

Open PowerShell ISE or PowerShell command line and type: get-host | select version

Sample Output:
Version: 2.0

Why need to check the PowerShell version?

If some commands that you find online is not working, it's either the command is wrong or the command is available for specific versions only.

New commands will work on new version of course, old commands may not work also on newer version if the command is obsolete or deprecated.

As the odds say, "Change is the only permanent thing in this world." and this quote is quite applicable in today's digital world.


Till next time. Cheers!

================================ Free Android Apps:
Click on links below to find out more:
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot.sg/2014/09/how-to-recite-rosary.html
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Excel Keyboard shortcuts guide http…

How to get uptime in Windows using PowerShell

If you need to monitor how long the machine has been up, or whether the machine rebooted by itself or someone has just rebooted the server for without any reason. Then getting the uptime of the machine or server is important.
PowerShell code below gets the uptime by using a command line and processing its output.
#==========================
#get the uptime and save to text file Invoke-command -ScriptBlock { net statistics server >c:\online.txt }  $xcmd = select-string -path "c:\online.txt" -Pattern "since" | select line |ft -hidetableheaders  write-output $xcmd | out-file c:\online1.txt  $stry = get-content c:\online1.txt | out-string  $concatString = $stry.Substring(18,15) Write-host  $concatString
#Get the date $StartDate= [datetime] $concatString $EndDate= (GET-DATE) $xdate = NEW-TIMESPAN –Start $StartDate –End $EndDate write-output $xdate #==========================
Sample output:
Days              : 3 Hours             : 12 Minutes           : 12 Seconds           : 17 …