Wednesday, February 8, 2012

PowerShell Cmdlets

Here are some PowerShell cmdlets that others might find helpful (as they are very much helpful to me). For more details, you may refer to http://technet.microsoft.com/en-us/library/ee156818.aspx.

Cmdlet
Usage
Example
Get-ChildItem Returns items in the specified folder Get-ChildItem $folder -include *.cs -force -recurse
Copy-Item (cpi) Copy files or folders Copy-Item $folder/$file -force -recurse
cpi $folder/$file -force -recurse
Remove-Item (ri) Delete files or folders Remove-Item $folder -force -recurse
ri $file -force
New-Item (ni) Create new files or folders New-Item $folder -type directory
ni $file -type file -force
Invoke-Item (ii) Run executable file or open a file. Alternatively prefix the script with an ampersand (&) and make sure to enclose parameters with spaces in double quotes Invoke-Item $script
ii $script
& $script "parameter1" "parameter2"
Get-Location (gl) Returns the current folder $currentDirectory = Get-Location
Set-Location (sl) Change the current folder Set-Location $newFolder
sl $newFolder


*joychua97

Running my very first PowerShell script

Ok, I browsed through the cheat sheet, and I feel that I am now ready to create my very first simple PowerShell script.

Opened up notepad to print a simple "Hello, World!" message.








Saved the file as sample.ps1, run Windows Powershell, changed my folder to where the script is, and ran sample.ps1. What I got were errors:

File C:\scripts\sample.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-
help about_signing" for more details.


And

PS C:\scripts> sample.ps1
The term 'sample.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:11
+ sample.ps1 <<<<
    + CategoryInfo          : ObjectNotFound: (sample.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException



Suggestion [3,General]: The command sample.ps1 was not found, but does exist in the current location. Windows PowerShell
 doesn't load commands from the current location by default. If you trust this command, instead type ".\sample.ps1". See
 "get-help about_Command_Precedence" for more details.
PS C:\scripts>


It turns out that running a PowerShell isn't how we're used to in running applications.  Here's how to successfully run the script:
1) Set the execution policy to RemoteSigned so you can run scripts, as the default is Restricted.
2) As  recommended by the error message above, we need to run the script indicating it's full path, or if you're in the same folder where the script is, prefix the script name with ".\"
3) Now if you want to run the script using the full path, and the path happens to have spaces (like C:\My Scripts\sample.ps1), then you'll run into the second error message above. If this is the case, make sure to enclose the full path with double quotes and prefix it with an ampersand (&).

PS C:\ scripts> & "C:\My Scripts\sample.ps1"


Reference: http://technet.microsoft.com/en-us/library/ee176949.aspx

*joychua97

Learning Windows PowerShell

Now I need to do some Windows scripting, and will have to learn Windows PowerShell. I found a good initial reference which is a cheat sheet that you can download from Developer Zone. You will have to register first (free) before you can download this pdf.



Download: http://refcardz.dzone.com/refcardz/windows-powershell

Reference: http://blogs.msdn.com/b/powershell/archive/2008/05/30/new-free-windows-powershell-quick-reference-card.aspx









*joychua97

Tuesday, February 7, 2012

Microsoft.Silverlight.CSharp.targets was not found

Here is the error that I was mentioning in my previous post.

The imported project "C:\Program Files\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.CSharp.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

Resolution:

1) If there are multiple build servers, determine which server is being used. You can do this by queueing a new build with Logging Verbosity set to "Diagnostic". This will also show the values of the MSBuild variables.

MSBuildExtensionsPath32 = C:\Program Files (x86)\MSBuild
MSBuildExtensionsPath64 = C:\Program Files\MSBuild
MSBuildExtensionsPath = C:\Program Files (x86)\MSBuild

COMPUTERNAME = <computer name>



2) Check which variable is defined in the project (example line below) and verify if the file exists. Also check if Silverlight 4 SDK is installed in the build server.

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Silverlight\$(SilverlightVersion)\Microsoft.Silverlight.CSharp.targets" />


3) If the file doesn't exist, install Silverlight 4 SDK in the build server, and that should solve it. Note that the build server doesn't have Visual Studio, and the Silverlight SDK installed without problems.

*joychua97

Fixing "The Silverlight 4 SDK is not installed" error

I need to troubleshoot a build error (will have a different post once I resolve that one), so I created a sample Silverlight application. It built and ran successfully in my machine, although I got the following error when I ran a build. Note that the build server already has Silverlight 4 SDK installed.

C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.Common.targets (104): The Silverlight 4 SDK is not installed.

Looking around, they said that "Silverlight projects cannot be built by 64-bit MSBuild."

To resolve this:
1) Edit your Build Definition, and go to the Process tab
2) Expand the "3. Advanced" section and set the "MSBuild Platform" value to "x86"
3) Save the Build Definition.


Changing the MSBuild Platform to x86




















Reference: http://social.msdn.microsoft.com/Forums/en/msbuild/thread/20a742a1-b99c-4638-8590-9a9d6a95fb69


Now I can move on to the original build error that I need to work on :)

*joychua97