How to install sharepoint development kit labs - powershell

I tried running one of the .bat files with CMD.
The first line it works
powershell -Command "Set-ExecutionPolicy Unrestricted"
powershell -Command "& {.\SetupLabxx.ps1}" -NoExit
pause
The second line gives me this exception
The term '.\SetupLabxx.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 pat h was included, verify that the path is correct and
try again. At line:1 char:20 + & {.\SetupLabxx.ps1 <<<< } -NoExit +
CategoryInfo : ObjectNotFound: (.\SetupLabxx.ps1:String) [], Co
mmandNotFoundException + FullyQualifiedErrorId :
CommandNotFoundException

You can combine the commands (use full path to script):
powershell.exe -NoExit -ExecutionPolicy Unrestricted -File c:\SetupLabxx.ps1

Related

Batch File Error: "The string is missing the terminator: '. " for a Powershell Cmd

Background
I have a batch file that is running a PowerShell Script. I've added this batch file to the startup folder of approximately 60 windows computers which are running either the W7 or W10 OS. It runs successfully on 95% of the PCs. On the remaining 5%, the batch file doesn't run and the error that's outputted is shown below.
The string is missing the terminator '.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Code
This is the code I've written on the batch file.
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%UserProfile%\Desktop\Test.ps1""' -Verb RunAs}"
Question
What can I do to fix this error and have the batch file run successfully on all computers?

Running a power shell command from batch files throws exception

When I run the below powershell command from powershell to get the app version, I get the correct response
(Get-Item "C:\\Program Files\\CompanyName\\AppName\\bin\\AppLauncher.exe").VersionInfo.FileVersion
However I need to run it from batch file so I add the following in my batch file.
powershell -Command " & (Get-Item 'C:\\Program Files\\CompanyName\\AppName\\bin\\AppLauncher.exe').VersionInfo.FileVersion"
On executing this, I see the version being returned on the console but with exception
& : The term '1.2.0.33' 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:4
+ & (Get-Item 'C:\\Program Files\\CompanyName\\AppName ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (1.2.0.33:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
'1.2.0.33' in the above error is the version I am expecting it to return.
Also if it could be suggested as to how to get this assigned to a variable.
You should be able to use either:
#%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"%ProgramFiles%\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"
Or:
#%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"$Env:ProgramFiles\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"
If you want to save it as a variable then perhaps:
#For /F %%G In ('%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"%ProgramFiles%\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"') Do #Set VerInfo=%%G
or:
#For /F %%G In ('%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "(Get-Item \"$Env:ProgramFiles\CompanyName\AppName\bin\AppLauncher.exe\").VersionInfo.FileVersion"') Do #Set VerInfo=%%G

How to execute command in Powershell 6 (core) from within Powershell 5?

I wish to run the following command from within Powershell 5 but make it execute with Powershell 6:
$PSVersionTable.PSVersion.Major
How can this be done?
The expected return value from the command should be '6'.
I am getting this:
pwsh -command "$PSVersionTable.PSVersion.Major"
System.Collections.Hashtable.PSVersion.Major : The term 'System.Collections.Hashtable.PSVersion.Major' 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:1
+ System.Collections.Hashtable.PSVersion.Major
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (System.Collections.\u2026ble.PSVersion.Major:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The problem is the double-quotes. PowerShell 5 is using string interpolation on the string before passing it to pwsh. So, what you see as:
pwsh -command "$PSVersionTable.PSVersion.Major"
Is actually passed as:
pwsh -command "System.Collections.Hashtable.PSVersion.Major.PSVersion.Major"
The solution is to use single quotes (which 'switches off' interpolation):
pwsh -command '$PSVersionTable.PSVersion.Major'

Set the font type and size using the command prompt (or batch file)

I tried the solution given from: Specify the size of command prompt when executing a batch file
I ran:
powershell -command "&{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But I get these errors, any ideas?
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. At line:1 char:22 + &{set-executionpolicy <<<< remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Import-Module : The specified module 'SetConsoleFont' was not loaded because no valid module file was found in any module directory . At line:1 char:50 + &{set-executionpolicy remotesigned; Import-Module <<<< SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : ResourceUnavailable: (SetConsoleFont:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
The term 'Get-ConsoleFontInfo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spe lling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:86 + &{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo <<<< | Format-Table -AutoSize} + CategoryInfo : ObjectNotFound: (Get-ConsoleFontInfo:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
I have put the file SetConsoleFont.psm1 in
C:\Users\Adrian\Documents\WindowsPowerShell\Modules\SetConsoleFont
You say "You're not allowed to set the execution policy" well maybe I'm not, but it's my machine so why shouldn't I? I don't want to execute these commands as Administrator, just as a user, me (Adrian)
Another comment was to try set-executionpolicy bypass process
so I tried:
powershell -command "&{set-executionpolicy bypass process; set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But got even more red errors.
I have no idea what powershell is or how to use it, I just want to change the font from a batch file without hassle!
Try set-executionpolicy bypass process instead.
Also make sure you have put the module in a module path folder such as:
[yourprofile]\Documents\WindowsPowershell\Modules
I managed to get it working but only in a PowerShell console, and I had to run it as Administrator. However this is not practical for me for the following reasons:
I wish to change the font of new window seamlessly from a batch file, which will be run by users of the software. They may not have Administrator access and so cannot execute "set-executionpolicy remotesigned" which I needed to do to get it working.
Also this has to be done in a DOS batch file, so opening up a powershell window is not an option. It only works in a PowerShell window and not with the DOS "powershell -command" option.
So a partial answer.
If you want to change Execution Policy, it should be done in an elevated prompt.
And loading the module can be done by giving absolute path. Example is below.
Import-Module c:\users\testuser\desktop\SetConsoleFont.psm1 -Verbose
and we can bypass execution policy like below.
powershell.exe -executionpolicy bypass -command "${<your code>}"
Edit: The imported module will be available only in the scope of the script block.
here it is with in {}. So whatever cmdlets and functions in side the module should be executed in sided the scriptblock.
Regards,
Kvprasoon

I keep receiving this error when trying to enable the execution of powershell scripts

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\
PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<< Unrestricted
+ CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAcce
ssException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.C
ommands.SetExecutionPolicyCommand
Is there any way to bypass this? Or at least run my powershell code? Please help!
you can do the following,
PowerShell.exe -ExecutionPolicy Bypass -File .runme.ps1
and run this in a cmd prompt
or copy and paste the whole script in a PS Console