Showing posts with label Powershell Advanced. Show all posts
Showing posts with label Powershell Advanced. Show all posts

Saturday, May 11, 2019

#92: How to forecast the time for certain task in Powershell?

This has always been a thought since I started using Windows 98. I remember the flying files avi file which used to pretend that actual file is flying from one folder to another. It used to appear as if the correct number of files are shown. Along with that it used to show how much time it will take. All in all, it was very amusing to see when I saw for the first time. I can't forget the Windows 98 themes in Plus. They all were full loaded with so much exciting stuffs into them. Under water them was the one which used to sound at every click and when application closes, it used to sound as if water went out.

Today's tip is not about Plus theme. This is all about forecasting the time which will be required to run a command. The time might vary based on situations, but you can get some guess about time. I will use measure-command cmdlet which will show you the time required to copy a file :

$SOURCE_FILE="C:\abc.zip"
$DESTINATION_FILE="D:\abc.zip"
(Measure-Command { Copy-Item $SOURCE_FILE $DESTINATION_FILE}).totalseconds

In my case, it output as :
1.540063

My file is around 50 MB and the location is same computer, so 1.5 seconds should be correct. I run the command as well and I suppose it took this much time.

This will work for all native commands only, you cannot run some other application and try to guess the time taken. Do not try to run something on SQL Server or any other application, this is good for native applications only.


Enjoy scripting!!

Sunday, April 26, 2015

#60 : Script to write into Excel Sheet

SUMMARY

In some cases, we need to create excel and upload as attachment to mail. This tasks is so simple when done manually, but we cannot be doing the same work everyday if we need to send Excel sheet everyday. We can try to automate it. But the automation should be as accurate as manual. It sounds very good, but sending a formatted excel document is not so easy.

We can simply attach a CSV which will automatically open in Excel, but CSV is not good for below reasons :

1. CSV does not show columns in correct length.
2. CSV does not color certain fields.
3. CSV will not show bold or italics.

So, the purpose will not be solved with CSV. So here I will use Microsoft ACE to generated the formatted report.

CODE




CONCLUSION

This article is still being written.
 

Sunday, April 19, 2015

#54 : How to upload file to SharePoint with Powershell?

Uploading to SharePoint is fairly simple with the below snippet. I am sharing the Gist script which might be useful for you. The only carefulness required here is to make sure that credentials are properly granted to the executing account.



Try above and let me know how it goes. Please update me in case of any issues.

Thanks for reading this article!

Thursday, April 16, 2015

#51 : How to check if Port if open or not?

This is fairly simply to test if certain host is able to connect to a port or not with Telnet. Some of the new SQL Server installation do not have Telnet installed. In those cases, we can use Powershell to test is the port.


I will use System.Net.Sockets.TcpClient class to accomplish it. Below is the simple statement to get the Port status.

$tcp = New-Object System.Net.Sockets.TcpClient

$tcp.connect(“HOST1″ , 1234 )


The above results as blank where connection is successful, otherwise, you get an error message.


Thursday, April 2, 2015

#50 : How to check with Powershell that URL is responsive or not?

You can write script in Powershell to check the status of certain URL (Uniform Resource Locator). In some cases, we may need to monitor if certain intranet application is working or not. Sometimes SQL Server Reporting services are required to be checked if URL is responsive.



To use it, you may call the function like below :

Check-URL "http://yourapplicationname.com"


That’s it! You are done and nothing more is required.

Friday, March 6, 2015

#47 : Creating modules with Powershell

Powershell modules are extension to inbuilt cmdlets. You may download third-party modules or you can write them using C#. Along with some installation, modules are installed in Powershell.

List the modules available: 

Use get-module to list the modules.

Import an existing module: 

import-module <module_name>

Writing your own module with Powershell

You can write your own module in Powershell using below steps:
1. Create a file with below content: 

function do-nothing()
{
echo "You are in nothing"
}

function do-something( $i, $j)
{
echo "Result : $($i + $j)"
}

2. Save the file with .psm1 extension. I use test.psm1 as filename.

3. Import the module 

import-module c:\tst\test.psm1

4. Check if module is loaded

PS D:\Org\Powershell> Get-Module

ModuleType Name ExportedCommands
---------- ---- ----------------
Script test {do-something, do-nothing}


5. Run the command used 

PS D:\Org\Powershell> do-something 5 6
Result : 11


CONCLUSION

Overall, we have below advantages using this approach:
1. Increases code manageability
Any changes made in one module is copied over all the modules.

2. Increases code reusability :
You can use common tasks like sending mails or logging in one module and share in all the scripts.

3. Team work
If a team of people working, working in modules reduces burden on single person and everyone owns his own part.

Thursday, February 26, 2015

#46 : Get Cluster details with Powershell

Windows Cluster can be found in every enterprise. We want to have high availability of our server application which increases the scope of having clusters. We are talking about Windows clusters only, your requirement might not match if you are using third-party clusters by Veritas and others.

My requirement of getting cluster related information was different but it might suit any Windows administrator. Basically, I am DBA Engineer and I was writing script which could collect SQL Server cluster information. So it required to know if there are below resources available:

1. Network resource
2. Disk Resource
3. Nodes

PREREQUISITES

We are going to use cmdlets which are not available in Powershell as native. Yeah, you understood - we are going to import module. But I am not talking about any third party material (which I don't like). The module is provided by Microsoft itself. If you have Windows 2008 and above, you might find the module already. For Windows 2003 and previous versions, I will let you know different set of commands (I don't leave any stone unturned).

INITIAL STEPS

Step [1] Find if module is available
Please run below command to list all the modules.

get-module -listAvailable

Step [2] Import the Module

import-module failoverclusters

If you are writing any script, make sure you import the module.

ACTION 

1. Find the Network resource

Here, I will just tell you the cmdlet to know what all network resource names can be seen on Windows cluster.

Get-ClusterNetwork -Cluster <cluster name>

2. Find the Disk resource 

Get-ClusterResource -Cluster <Cluster name>

3. Cluster nodes info

Get-ClusterNode -Cluster <Cluster Name>

4. Find Cluster groups available

Get-ClusterGroup -Cluster <Cluster Name>

Below is the list of cmdlets which can help you manage cluster:

Add-ClusterDisk
Make a new disk available for use in a failover cluster. The disk (LUN) must be exposed to all nodes in the failover cluster, and should not be exposed to any other servers.

Add-ClusterFileServerRole
Create a clustered file server (resource group that includes one or more disks, on which you can create shared folders for users).

Add-ClusterGenericApplicationRole
Configure high availability for an application that was not originally designed to run in a failover cluster.

Add-ClusterGenericScriptRole
Configure an application controlled by a script that runs in Windows Script Host, within a failover cluster.

Add-ClusterGenericServiceRole
Configure high availability for a service that was not originally designed to run in a failover cluster.

Add-ClusterGroup
Add an empty resource group to the failover cluster configuration, in preparation for adding clustered resources to the group.

Add-ClusterNode
Add a node (server) to a failover cluster. Before adding the new node, you should run validation tests on the existing nodes together with the proposed new node.

Add-ClusterPrintServerRole
Create a clustered print server (a resource group that includes a printer and a disk for storing print job information and printer drivers).

Add-ClusterResource
Add a resource to a clustered service or application (resource group) in a failover cluster.

Add-ClusterResourceDependency
Add a resource to the list of resources that a particular resource depends on (using AND as the connector) within a failover cluster. Existing dependencies will remain in the list.

Add-ClusterResourceType
Add a resource type to a failover cluster, and specify information such as the dynamic-link library (DLL) to use with that resource type.

Add-ClusterServerRole
Add a group containing only a client access point and storage to the failover cluster configuration.

Add-ClusterSharedVolume
Make a volume available in Cluster Shared Volumes in a failover cluster.

Add-ClusterVirtualMachineRole
Create a clustered virtual machine, that is, a virtual machine that can be failed over if necessary to a different server in the failover cluster.

Block-ClusterAccess
Prevent the specified user or users from accessing a failover cluster.

Clear-ClusterDiskReservation
Clear the persistent reservation on a disk in a failover cluster.

Clear-ClusterNode
Clear the cluster configuration from a node that was evicted from a failover cluster.

Get-Cluster
Get information about one or more failover clusters in a given domain.

Get-ClusterAccess
Get information about permissions that control access to a failover cluster.

Get-ClusterAvailableDisk
Get information about the disks that can support failover clustering and are visible to all nodes, but are not yet part of the set of clustered disks.

Get-ClusterGroup
Get information about one or more clustered services or applications (resource groups) in a failover cluster.

Get-ClusterLog
Create a log file for all nodes (or a specific node) in a failover cluster.

Get-ClusterNetwork
Get information about one or more networks in a failover cluster.

Get-ClusterNetworkInterface
Get information about one or more network adapters in a failover cluster.

Get-ClusterNode
Get information about one or more nodes (servers) in a failover cluster.

Get-ClusterOwnerNode
For a resource in a failover cluster, get information about which nodes can own the resource. For a clustered service or application (a resource group), get information about the order of preference among owner nodes.

Get-ClusterParameter
Get detailed information about an object in a failover cluster, such as a cluster resource. This cmdlet is used to manage private properties for a cluster object.

Get-ClusterQuorum
Get information about the quorum configuration of a failover cluster.

Get-ClusterResource
Get information about one or more resources in a failover cluster.

Get-ClusterResourceDependency
Get information about the dependencies that have been configured between clustered resources in a failover cluster.

Get-ClusterResourceDependencyReport
Generate a report that lists the dependencies between resources in a failover cluster.

Get-ClusterResourceType
Get information about one or more resource types in a failover cluster.

Get-ClusterSharedVolume
Get information about Cluster Shared Volumes in a failover cluster.

Grant-ClusterAccess
Grant access to a failover cluster, either full access or read-only access.

Move-ClusterGroup
Move a clustered service or application (a resource group) from one node to another in a failover cluster.

Move-ClusterResource
Move a clustered resource from one clustered service or application to another within a failover cluster.

Move-ClusterSharedVolume
Move a Cluster Shared Volume to ownership by a different node in a failover cluster.

Move-ClusterVirtualMachineRole
Move the ownership of a clustered virtual machine to a different node.

New-Cluster
Create a new failover cluster. Before you can create a cluster, you must connect the hardware (servers, networks, and storage), and run the validation tests.

Remove-Cluster
Destroy an existing failover cluster. The affected servers will no longer function together as a cluster.

Remove-ClusterAccess
Remove a user from the access list on the cluster.

Remove-ClusterGroup
Remove a clustered service or application (also called a resource group) from a failover cluster.

Remove-ClusterNode
Remove a node from a failover cluster. After the node is removed, it no longer functions as part of the cluster unless you add it back to the cluster.

Remove-ClusterResource
Remove a clustered resource from the failover cluster.

Remove-ClusterResourceDependency
Remove a dependency between two resources in a clustered service or application within a failover cluster.

Remove-ClusterResourceType
Remove a resource type from a failover cluster.

Remove-ClusterSharedVolume
Remove a volume from the Cluster Shared Volumes in a failover cluster, and place it in Available Storage in the cluster.

Repair-ClusterSharedVolume
Run repair tools on a Cluster Shared Volume locally on a cluster node.

Resume-ClusterNode
Resume activity on a failover cluster node after you have suspended it (that is, paused it).

Resume-ClusterResource
Turn off maintenance for a disk resource or Cluster Shared Volume within a failover cluster.

Set-ClusterLog
Set the size and level of detail for the cluster log.

Set-ClusterOwnerNode
For a resource in a failover cluster, specify which nodes can own the resource. For a clustered service or application (a resource group), specify information about the order of preference among owner nodes.

Set-ClusterParameter
Control specific properties of an object in a failover cluster, such as a resource, a group, or a network.

Set-ClusterQuorum
Configure quorum options for a failover cluster.

Set-ClusterResourceDependency
Specify the resources that a particular resource depends on within a failover cluster. Existing dependencies will be overwritten by the dependencies that you specify.

Start-Cluster
Start the Cluster service on all nodes of the cluster on which it is not yet started.

Start-ClusterGroup
Bring one or more clustered services and applications (also known as resource groups) online on a failover cluster.

Start-ClusterNode
Start the Cluster service on a node in a failover cluster.

Start-ClusterResource
Bring a resource online in a failover cluster.

Stop-Cluster
Stop the Cluster service on all nodes in a failover cluster, which will stop all services and applications configured in the cluster.

Stop-ClusterGroup
Take one or more clustered services and applications (also known as resource groups) offline on a failover cluster.

Stop-ClusterNode
Stop the Cluster service on a node in a failover cluster.

Stop-ClusterResource
Take a resource offline in a failover cluster.

Suspend-ClusterNode
Suspend activity on a failover cluster node, that is, pause the node.

Suspend-ClusterResource
Turn on maintenance for a disk resource or Cluster Shared Volume so that you can run a disk maintenance tool without triggering failover.

Test-Cluster
Run validation tests for failover cluster hardware and settings. Tests can be run both before and after a cluster is set up.

Test-ClusterResourceFailure
Simulate a failure of a cluster resource.

Update-ClusterIPResource
Renew or release the DHCP lease for an IP address resource in a failover cluster.

Update-ClusterVirtualMachineConfiguration
Refresh the configuration of a clustered virtual machine within a failover cluster

Equivalent commands for pervious version of Windwos 2003: 

Cluster.exe <Cluster Name> /prop
Get-Cluster <Cluster Name> | fl *

Cluster.exe <Cluster Name> /priv
Get-Cluster <Cluster Name> | Get-ClusterParameter

Cluster.exe <Cluster Name> group
Get-ClusterGroup -Cluster <Cluster Name>

Cluster.exe <cluster Name> node
Get-ClusterNode -Cluster <Cluster Name>

Cluster.exe <cluster name> res
Get-ClusterResource -Cluster <Cluster name>

Cluster.exe <Cluster name> net
Get-ClusterNetwork -Cluster <cluster name>

Cluster.exe <cluster name> netint
Get-ClusterNetworkInterface -Cluster <cluster name>

Cluster.exe <cluster name> group /move:<Node name>
Move-ClusterGroup -Name <group name> -Node <Node name> -Cluster <cluster name>

Cluster.exe log
Get-ClusterLog

Cluster.exe <cluster name> /create …
New-Cluster

Cluster.exe <cluster name> group /add
Add-ClusterGroup

Cluster.exe <cluster name> group <group name> /on
Start-ClusterGroup -Name <group name> -Cluster <cluster name>

CONCLUSION
As you can see the module failoverclusters gives you more and more options to manage cluster. Sorry, I could not add any script for this article, but you may ask me in case you want, I will reply once I get time.

Wednesday, February 4, 2015

#43 : Powershell Jobs

This is one of the complex topic and needs proper understanding before I start with how to achieve with Powershell.

PROCESS AND THREAD

A process is an instance of application which runs under its own virtual address space and runs independent of other processes.

A thread is a basic executable unit. Each process contain one or more threads running to perform specific parts of the process. A thread is scheduled on the basis of availability of resources such as CPU, Memory and others.

BENEFITS OF MULTI-THREADING

With the help of jobs, we can implement multi-threading with Powershell. It can be very much helpful for requirements like below-
1. Copying files to multiple servers parallely.
2. If the script runs on different host to collect some information, multi-threading can save a lot of time. For example, if each remote host takes 5 minutes to complete and we have 100 hosts, running them serially will take 500 seconds. Using multi-threading, you can do the same task within 5 minutes.

But we have to be sure that we have enough CPU to perform tasks in parallel. Especially file copy operations are memory consuming. So we will not throw everything in the same time, we will do the same task in sets, for example 5 servers in each set, once all of them complete, next set. Or else make sure 5 tasks to run a time, one any of the server completed, one more task will be created, but if all 5 are running, we will wait! Just thinking... Believe me there is no script written by this point of time.

DEPLOYMENT PLAN

There might be a core .NET method which might be useful in this case. I don't know, but I will deploy the same using Powershell jobs. Powershell can create, stop and check status of jobs created. The job should not be confused with SQL Server jobs or Windows Scheduler jobs. Powershell has the capability to create background jobs.

1. Each job runs under context of the same process. where it was started There is nothing like job server where you can control other jobs running on the host.

2. The failure of successs of jobs can be monitored with get-job cmdlet.

3.
start-job : Starts the job
stop-job : Stops a job
get-job : Shows the status of job
receive-job : Shows the output of command run with the job
(receive-job can be useful in some other cases to store results temporarily).

4. A job does not output anything in main console. This applies to failure of job also. The output is only received when the job completes.

EXPERIMENTING WITH JOBS
1. Start the job
start-job -Name "Testing" -ScriptBlock {get-process}

2. Monitor the status of job
get-job

3. Using Job Object
get-job has Id of each job. This Id can be achieved by running a line something like this:

$jobId = start-job -Name "Testing" -ScriptBlock {get-process}
-OR-
Start the job, run get-job to get the job id to look for.

$job = get-job -id 2

$jobId can be used further as an object for the job and you can get entire information about the job:

$jobId.Name
$jobID.Status

4. receive-job to know the results:
recieve-job -id $jobId

Tuesday, November 25, 2014

#33 : Complete script to download file with Powershell

Powershell can be used to download some files from internet or intranet. We can simply do it with Invoke-WebRequest.

Below is the code which can be used for this purpose :

#--------------------------------------------------------------------------------------------------------------
#-- Script to Download file 
#--------------------------------------------------------------------------------------------------------------

function Download-File ([string]$url , [string]$target)
{
 trap {
  echo "Error occured" 
  continue 
 }
 
 if ( !(Test-path $target ))
 {
  invoke-webrequest $url -OutFile $target
 }
 else 
 {
  echo "Error : Directory does not exit!"
 }

}

#--------------------------------------------------------------------------------------------------------------

Download-File "http://site.foo.com/file.exe" "C:\temp"

#--------------------------------------------------------------------------------------------------------------

Enjoy!

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...