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

Thursday, 4 August 2011

ASPX page continuously asks for credentials.

I came across the error The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception. when trying to compile the c# in ASP.NET code using Visual Studio.

Solved
Open Control Panel > Install / Uninstall Programs > .NET 3.5 SP1 > Repair

Tuesday, 5 July 2011

Get assembly fully qualified name

While doing development it has occurred to me many times how to get the assembly name easily of a dll(that is strong named). The normal approach is to create it manually from the properties of the dll file.

I would like to share with you a very eazy way to get the fully qualified assembly name using Visual Studio. It uses the external tools feature of Visual Studio.

  1. Open Visual Studio
  2. Go to Tools –> External Tools
  3. Click on "Add" to add a tool, and put in the following values:
    1. Title: Get Qualified Assembly Name
    2. Command: Powershell.exe
    3. Arguments: -command "[System.Reflection.AssemblyName]::GetAssemblyName(\"$(TargetPath)\").FullName"
    4. Check "Use Output Window". All other checkboxes should be unchecked
The new tool appears under Tools –> Get Qualified Assembly Name.

When this menu item is selected, the assembly name is given in the output window.
It should look like HelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b2135059549c7590

Wednesday, 29 June 2011

Backup a file using powershell script

Some critical files that I modify each day had to be backed up to a secure location, in case the file gets corrupted I can go to the archive and get the most recent backup.

I created a powershell script to do this. The script is pasted below.
#file name backup.ps1
$PathToGoalsSheet="C:\preformancereports\Akshay"
$FileToBeBackedUp="Goals.xlsx"
$BackupPath="D:\myComany\Dept\Backup"
$TodaysDateFormatted = Get-Date -format dd.MM.yyyy.HH.mm.ss
$BackupFolderName=[string]::Format("{0}.backup",$TodaysDateFormatted)
#Write($BackupFolderName)
if(Test-Path $PathToGoalsSheet)
{
if(Test-Path $PathToGoalsSheet\$FileToBeBackedUp)
{
Write("Creating folder ["+$BackupFolderName+"]...")
New-Item $PathToGoalsSheet\$BackupFolderName -type directory
Write("Copying File ["+ $FileToBeBackedUp +"] into ["+$BackupFolderName+"]...")
Copy-Item "$PathToGoalsSheet\$FileToBeBackedUp" "$BackupPath\$BackupFolderName"
}
else
{
Write("Could not find file ["+ "$PathToGoalsSheet\$FileToBeBackedUp" +"]!")
}
}
else
{
Write("The path ["+$PathToGoalsSheet+"] does not exist!")
}

The command to run this script file, I typed in a batch file (.bat), so I can run the powershell file (or schedule a task to run the batch file on a daily basis). The code in the batch file is as given below.

rem file name backup.bat
@echo off
set scriptHomePath=C:\PowerShell Scripts\
set backupFileScript=%scriptHomePath%backup.ps1
powershell -NoLogo -File "%backupFileScript%"