Advertisements

Category Archives: Powershell

Powershell – Get the First and Last Day of the Month

You could do this in different ways with powershell. But the whole idea is to keep it simple

$CURRENTDATE=GET-DATE -Format "MM/dd/yyyy"
$FIRSTDAYOFMONTH=GET-DATE $CURRENTDATE -Day 1
$LASTDAYOFMONTH=GET-DATE $FIRSTDAYOFMONTH.AddMonths(1).AddSeconds(-1)

$FIRSTDAYOFMONTH
$LASTDAYOFMONTH
Advertisements

Send-MailMessage – Send Mail with multiple attachments

Powershell – Send Multiple Email Attachments using Send-MailMessage

Pass each attachment location to a variable and add this to the Attachments parameter separated by comma

Example

$Att1="c:\test1.csv"
$Att2="c:\test1.csv"
Send-MailMessage -To abc@tkr.com - From xyz@tkr.com -Subject "Attachments" -Attachments $Att1,$Att2

Manage Storage of IIS Log Files – Scheduled Deletion of IIS Logs

Drive Space Full with IIS Log Files / Reclaim Disk Space

Are your Local Drives of your IIS Server running out of space? Have you checked the size of your IIS Logs?

If your IIS was setup by a professional; you would not have to go through this issue. The log files would have been managed appropriately

Ideally I do not see a purpose to keep IIS Log files beyond a period of 1 month. Those older than one month should ideally be removed to prevent disk space issues

I found below posts which will assist you. Before you use the below scripts; please identify the location of your log files

Using Powershell

http://www.codeproject.com/Articles/663000/Simple-Powershell-script-to-clean-up-IIS-log-files

Using VBScript

http://www.iis.net/learn/manage/provisioning-and-managing-iis/managing-iis-log-file-storage#02

By default; you would find them on C:\inetpub\logs\LogFiles\

To verify the location; open IIS and select the Default Website and click Logging

IIS_Screenshot

Take note of the path mentioned in the directory field

IIS_Logging

Powershell Script – Not digitally signed

Unable to Run Powershell Script – Error: Not digitally signed

Error: The file *.ps1 is not digitally signed. The script will not execute on the system

+FullyQualifiedErrorID: UnauthorizedAccess

To resolve above error; set the execution policy to unrestricted

Set-ExecutionPolicy -ExecutionPolicy Bypass

Powershell – Export-Csv to export multi-valued attributes

Passing Mutliple Variables to Export-CSV cmdlet

If you have overcome a situation where-in; while exporting results using Export-CSV; multi-valued columns return just the property System.String[]; I have just the right solution for you

If the multi-valued column name is recipients; use the column as below

@{Name=’recipients’;Expression={[string]::join(“;”, ($_.recipients))}}

 

Powershell – Display command output one page at a time

Please note that the below command applies for use in the Windows Powershell Console and not in the Windows Powershell ISE

Use the More function to achieve the page wise output

Get-Service -ComputerName computername | more

 

Powershell Error – Index was outside the bounds of the array when using System.Net.Mail Class with Send-MailMessage cmdlet

Are you using the System.Net.Mail to send File Attachments? Is the file attachment above 3MB?

If yes; you have just found out the reason for the error. The System.Net.Mail Class throws an exception if a file attachment is over 3MB

Microsoft has a patch available which solves this issue

https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=30226

If you have any issues downloading the file; you may post your comment on this post.

Once the patch is applied; you should now be able to send large attachments using your Powershell Send-MailMessage cmdlet.

Please ensure that you use the -UseSSL in your cmdlet. If you leave out -UseSSL; you are most likely to encounter the below error

Send-MailMessage : The operation has timed out.

 

Get-WmiObject : The RPC Server is unavailable

Error Message when running Get-WmiObject cmdlet on a Remote Machine

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:1
+ Get-WmiObject win32_bios -ComputerName remotecomputername|Select PSComputerName,Manufact ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
 + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand


Verify DCOM Settings on Remote Machine

  • The first step is to check is if the DCOM Service is enabled in the Remote Machine
    • From the remote machine; From the Start Menu; type dcomcnfg
    • Open Computers – Right Click My Computer – Properties
    • If the checkbox for ‘Enable Distributed COM on this computer ‘ is ticked; you have got through the first step. By default this option is always enabled

DCOM_Settings

 

Check the Windows Firewall on Remote Machine

  • The next step is to check Windows Firewall. It is most likely that your firewall is enabled.
  • For testing purposes, please disable the Firewall on the remote machine and test the script. I’m sure you have got the result. If not, I would sadly request you to look for answers elsewhere 😦
  • It is not advisable to disable your Firewall to get this working and most of all if you are administering a network of over 100 or 1000 machines this is not practical either. So the best way is to identify the Communication Port that was blocked when running the script. So turn back your Firewall On.

Enabling Logging on Windows Firewall and Identify Blocked Port on Remote Machine

  • From Advanced Settings of your Firewall; right click Windows Firewall with Advanced Security on Local Computer and select Properties
  • From the Domain Profile Tab; click Customize under Logging
  • Select the Yes drop-down for Log dropped packets
  • Take note of the Logfile Location and filename from the name field
  • Click Ok to confirm

Enable_Firewall_Logging

 

  • From your PC or a Server PC; run the Get-WmiObject cmdlet once again so as to receive the error
  • From the remote machine; run Notepad as Administrator and open the pfirewall.log file
  • Identify the Drop Action from the Firewall that points to the time we ran the Get-WmiObject cmdlet and the source-ip (src-ip) of the machine you ran the script from
  • Take note of the Protocol TCP/UDP and Destination Port Number (dst-port) Eg: 49155
  • You can enable an Inbound Rule for this your problem will solved. If you are administering a large network; apply this to a group policy instead.

Inbound_Rule

Unable to Install Windows Management Framework 3 on Windows 2008 SP2 Server

Are you getting the below error when installing Windows Management Framework 3 on your Windows 2008 SP2 Server?

Update_Not_Applicableu

 

Well, all you need to verify is the current version of your .NET Framework.  To verify the current version; proceed to the registry

HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP

The branch is most likely only upto V3 or V3.5

Download and Install .NET Framework 4 and your system will be ready to Install the Windows Management Framework 3

You may also find below post useful

http://www.happysysadm.com/2012/06/upgrading-to-powershell-30-on-windows.html

 

Write-EventLog The source name does not exist on computer

Write-EventLog : The source name “” does not exist on computer “”

If you have received this error using Powershell when trying to Write to the Event Log; it’s because you have not registered the source yet

Use the New-EventLog cmdlet to register the source prior to writing to the event log

New-EventLog -LogName Application -Source <Sourcename>