CSPack and CSRun for running site in azure emulator from powershell - powershell

I have spend some time trying to get the cspack and csrun command working to run a website locally in the azure emulator.
So far this is what I get, but its not working
I use psake
Task StartAzureEmulator {
& 'C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.2\bin\cspack' sitename.azure\ServiceDefinition.csdef /out:Sitename.csx /role:sitename;sitename /sites:Vola;Web;Web /copyOnly
& 'C:\Program Files\Microsoft SDKs\Windows Azure\Emulator\csrun' sitename.csx sitename.Azure\ServiceConfiguration.Local.cscfg /useiisexpress /launchbrowser
}
Basically I am not very impressed with how the azure commandline tools works with powershell.
Has anyone got example of this working?

In PowerShell the ; character is a statement separator. You can escape it by preceding it with a backtick or if you are V3 or higher you can user --% to switch PowerShell into a simpler (dumber) parser mode. Try this:
Task StartAzureEmulator {
& 'C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.2\bin\cspack' --% sitename.azure\ServiceDefinition.csdef /out:Sitename.csx /role:sitename;sitename /sites:Vola;Web;Web /copyOnly
& 'C:\Program Files\Microsoft SDKs\Windows Azure\Emulator\csrun' --% sitename.csx sitename.Azure\ServiceConfiguration.Local.cscfg /useiisexpress /launchbrowser
}

Related

How to uninstall VS Code from cli on Windows

I am trying to create a SCCM package for installing/uninstalling VS Code on Windows. I am able to install without any problems, but I cannot get it to uninstall.
Here is what I have tried:
For reference, VSCodeSetup-x64-1.28.0.exe is the executable I used for installing vs code.
From Powershell:
Start-Process -FilePath .\VSCodeSetup-x64-1.28.0.exe -ArgumentList "/uninstall" -Wait -PassThru (This just executes the installer with no options to uninstall)
Start-Process -FilePath "C:\Program Files\Microso
ft VS Code\Code.exe" -ArgumentList "/uninstall" -Wait -PassThru (This just opens VS Code)
I looked at the following site for command line options, but no mention of uninstall.
http://jrsoftware.org/ishelp/index.php?topic=setupcmdline
Looking at the uninstall string in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ (For me it was {F8A2A208-72B3-4D61-95FC-8A65D340689B}_is1 but this could be version dependent) it seems like there is a REG_SZ QuietUninstallString which in my case pointed to:
"C:\Program Files\Microsoft VS Code\unins000.exe" /SILENT
So you could either if those are all default instalations just hard code that path (relative to where you installed it so possibly program files (x86) or some other path) or read the string before uninstall in some script and use it directly (might also be affected by WoW64 I only did a quick test on a 32bit system)

Running cmd /c from PowerShell with spaces in filepath

I am trying to run the following command in PowerShell
PS C:\Users\Administrator> cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat && nuget restore && msbuild mywebapp.sln /p:DeployOnBuild=true /p:PublishedProfile=ServerFolderProfile"
This produces the error
'C:\Program' is not recognized as an internal or external command.
My paths have spaces and I'm running several commands seperated by && which is messing everything up. I have tried putting quotes all over the place but I can't get it to work.
If i run just the first part of the command
cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat"
it works fine. But I can't get the other commands to work too.
According to the docs for cmd.exe:
If /C or /K is specified, then the remainder of the command line is processed as an immediate command in the new shell. Multiple commands separated by the command separator & or && are accepted if surrounded by quotes
So I just had to change my command to:
PS C:\Users\Administrator> cmd /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\Tools\vsdevcmd.bat" "&&" nuget restore "&&" msbuild mywebapp.sln /p:DeployOnBuild=true /p:PublishedProfile=ServerFolderProfile

Cannot load SharePoint powershell cmdlets in visual studio pre-deployment script

While working on a SharePoint 2010 solution in Visual Studio 2012, I want to run a PowerShell script to remove a lookup field from one of my lists, allowing Visual Studio to automatically resolve the script deployment conflict by deleting the existing list and replacing it with the one in my solution.
However, when running PowerShell from the project's "Pre-deployment Command Line" in Visual Studio, when my script attempts to use get-spweb, PowerShell reports that the object is not found. Scrolling upward in the Visual Studio's output window, I see that Add-PsSnapin Microsoft.SharePoint.PowerShell is reporting various problems:
(Note: actual error message has the expanded $(ProjectDir) value rather than "$(ProjectDir)" as text. Trying various levels of indirection to ensure I'm using the correct, 64-bit version of PowerShell does not change this working directory, nor does using cd or set-location commands prior to calling my script make any difference in this directory. I'm wondering if an invalid working directory is part of the problem...)
The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.
Could not read the XML Configuration file in the folder CONFIG\PowerShell\Registration\.
Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\Registration'.
No xml configuration files loaded.
Unable to register core product cmdlets.
Could not read the Types files in the folder CONFIG\PowerShell\types\.
Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\types'.
"No Types files Found."
Could not read the Format file in the folder CONFIG\PowerShell\format\.
Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\format'.
No Format files Found.
Running the PowerShell script directly from a Command Prompt window works fine, the SharePoint snapin loads correctly, but running from Visual Studio always fails. Previous research indicated potential problems with SharePoint and SQL Server permissions, yet my account has full admin in both. Visual Studio is running "as administrator". Research also turned up possible problems with 32-bit vs 64-bit. My Pre-deployment Command Line now calls %comspec% /c to ensure 64-bit.
Pre-deployment Command Line:
%comspec% /c ""$(ProjectDir)PowerShell Scripts\predeployment-command.cmd" "$(SharePointSiteUrl)" "$(ConfigurationName)" "$(ProjectDir)""
*.cmd file:
echo off
rem pre-deployment script
rem call from pre-deployment command line like
rem %comspec% /c ""$(ProjectDir)PowerShell Scripts\predeployment-command.cmd "$(SharePointSiteUrl)" "$(ConfigurationName)" "$(ProjectDir)""
rem
echo Running "predeployment-command.cmd" file
echo
echo %%1 = %~1
echo %%2 = %~2
echo %%3 = %~3
echo
cd "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14"
cd
echo on
powershell.exe -file "%~3PowerShell Scripts\predeployment-script.ps1" -targetWeb "%~1" -CONFIG "%~2"
PowerShell script file:
<#
.SYNOPSIS
Pre-deployment script for Visual Studio development deployments
add command to run this script in the project's pre-deployment command-line box
.EXAMPLE
powershell .\dev-predeployment-script.ps1
#>
param(
[string] $targetWeb = $(throw "Please specify the site to which Visual Studio is deploying!"),
[string] $CONFIG = $(throw "Please specify the active configuration!")
)
write-host "Running Pre-Deployment PowerShell Script";
# Ensure SharePoint extensions are loaded
$snapin = $(Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue");
if($snapin -eq $null) {
Add-PsSnapin Microsoft.SharePoint.PowerShell;
}
$ErrorActionPreference = 'Stop';
#echo back parameter values:
write-host "TargetWeb = $targetWeb"
write-host "Configuration = $CONFIG"
#get web-site
$web = get-spweb $targetWeb;
if($web -ne $null) {
$recipients = $web.lists["Recipients"];
if($recipients -ne $null) {
$lookupField = $recipients.Fields["Request ID"];
if($lookupField -ne $null) {
$recipients.fields.remove($lookupField);
}
}
}
I'm developing on a 64-bit Windows Server 2008 r2 virtual machine.
Hi I know this was from a billion years ago, but I just struggled with and solved the same problem, so posting here in case others run into it.
Two problems are happening and fighting with each other:
Problem #01: Spaces in paths: At first glance I see a lot of 'em.
Make your life easier and get rid of these (if/when possible).
Examples:
"$(ProjectDir)PowerShell Scripts"
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14"
"$(SharePointSiteUrl)" "$(ConfigurationName)" "$(ProjectDir)" <-- This is the problem!!!
maybe others too? you've got multiple levels there that's hard to visualize.
Call chain is (I think?):
LVL1: %comspec% /c
LVL2: predeployment-command.cmd
LVL3: "%~3PowerShell Scripts\predeployment-script.ps1"
which you wish to be: '$(ProjectDir)PowerShell Scripts\predeployment-script.ps1'
but is likely rendering as: 'path with spaces"PowerShell Scripts\predeployment-script.ps1'
Problem #02: Visual Studio Paths always end in backslash "\" (which I hilariously have to escape so that the SO Markdown will display it). BATCH Expansion Escapes the next character. So if you've correctly wrapped quotes around a "path with spaces", but the "path with spaces ends in backslash\", then you end up with "...\" being escaped to %path%" with a trailing or hanging quote " at the end
Visual Studio $(ProjectDir) - with spaces!
if you DO NOT include the quotes, then spaces in path will break it. if you DO include quotes then it gets escaped.
Solution is to include an extra slash at the end, resulting in double backslash \\ which escapes down to single backslash '\'... whew!
Here's my working version if you'd like an example to work off of:
https://stackoverflow.com/a/73409081/738895
Final note:
IF all you need is to ensure (32-bit vs 64-bit), this can be done via the Configuration Manager in Visual Studio
You can google "Visual Studio force build as x86" if you need via msbuild/commandline or follow these instructions
https://learn.microsoft.com/en-us/visualstudio/ide/how-to-configure-projects-to-target-platforms?view=vs-2022

Not able to log the sql server installation progress when running with powershell

I am installing SQL Server 2008R2 with Powershell.
Below is the command
function install{
$Command ="SQL_SERVER_CDs\SQL2008_R2\R2_ENTERPRISE\Setup.exe /q /ACTION=Install /IACCEPTSQLSERVERLICENSETERMS /INDICATEPROGRESS /FEATURES=SQLENGINE,REPLICATION,FULLTEXT /INSTANCENAME=DEMO5 /SECURITYMODE=SQL /SAPWD=Me#inv2011 /SQLSYSADMINACCOUNTS=CORP\R-PITTUR CORP\AIMFUNDS-G-Admin-SQL /SQLSVCACCOUNT=RAMU-PC\RAMU /SQLSVCPASSWORD=***** /AGTSVCACCOUNT=RAMU-PC\RAMU /AGTSVCPASSWORD=***** /ISSVCACCOUNT=RAMU-PC\RAMU /ISSVCPASSWORD=***** /INSTANCEDIR=S:\\ /INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server /SQLUSERDBDIR=I:\DB_DATA /SQLUSERDBLOGDIR=H:\DB_LOGS /SQLTEMPDBDIR=T:\DEMO5 /SQLBACKUPDIR=S:\DEMO5 /SQLCOLLATION=SQL_Latin1_General_CP1_CI_AS"
Invoke-Expression $Command
}
When i call this install function, SQL installation is happenening but nothing is displayed in the Powershell console though i put Indicateprogress in the above installation string. How can we display logging of sql server installation. Is it possible to redirect the output to any control like Richtextbox using Powershell.
Some of your command options contain spaces, for eg:
/INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server
wrap them in quotes like this:
`"/INSTALLSHAREDDIR=C:\Program Files\Microsoft SQL Server`"
note the use of the backtick character (`) to escape the quote within the string.
/q means quite install, right?
Here's the command I use to run SQL 2008 R2 installs:
$command += 'setup.exe /CONFIGURATIONFILE=`"$configFile`" /SAPWD=`"$sysadminPassword`" /SQLSVCPASSWORD=`"$servicePassword`" /AGTSVCPASSWORD=`"$servicePassword`" /FTSVCPASSWORD=`"$servicePassword`" /ISSVCPASSWORD=`"$servicePassword`"'
Invoke-Expression $command

Preparting environment for running a powershell script

I needed to schedule a powershell script and before doing it, i needed to prep the environment by running some other powershell script.
Basically i wanted to run
PowerShell.exe -noexit " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\sharepoint.ps1' "
PowerShell.exe -command " & ' E:\PerfCounters\Powershell\RunPerf.ps1' "
The first statement will perp my environment and i want to execute my own script after that.
Issue is, if i use the -noexit command, the next script never gets executed. If i use -command instead of -noexit, the commands seems to be executed in different powershell instances so my second script is erroring out.
I am a newbee in powershell and i did my best tring to find a answer. Any help here would be really appreciated. thanks!
If you didn't solve this on your own, or if anyone else needs the syntax, here you go:
powershell -noprofile -command "& { & .\x.ps1; & .\z.ps1 }"