The term 'xsd' is not recognized as the name of a cmdlet, function, script file, or operable program - powershell

When executing the following script (this is a part of the actual script), I'm getting the following error message in powershell:
The term 'xsd' 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:10 char:3
$xsds = ls *.xsd | %{ $_.Name }
if ($xsds.Count -eq 0) { exit }
# Add '.\' to last file name, see http://stackoverflow.com/questions/906093/xsd-exe-output-filename
$last = $xsds | select -last 1
$last = '.\' + $last
$xsds[$xsds.Count - 1] = $last
& xsd $xsds /c /n:OutputFolder
Are there some requirements for Powershell that I need to install to be able to run the 'xsd' cmdlet first?
The output of $env:Path:
PS C:\Users\Administrator\Desktop\New> $env:Path
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Mi
crosoft\Web Platform Installer\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files
(x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Micro
soft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program F
iles (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\
PS C:\Users\Administrator\Desktop\New>
An xsd.exe is available in the folders:
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64

The paths you have listed are not part of your PATH environment variable. So that leaves you with two options. Add the directories to path or just reference the exe by its full path.
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\xsd.exe" $xsds /c /n:OutputFolder
If you want to change your paths you could update them like this
$env:Path += ";C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools"
If you need x64 paths just update the strings.

Related

How to revert Powershell PATH variable to the default value

I have mistakenly replaced the PATH variable instead of appending to it. How do I revert back to the default value of the PATH?
If we want to reset your PATH environment variable without restarting your PowerShell session, give this a try:
$Env:Path = [System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::Machine).Path + ';' + [System.Environment]::GetEnvironmentVariables([System.EnvironmentVariableTarget]::User).Path
To get the default current path run the following
($env:PATH).split(";")
if i understand your question that you have by mistake replace the path variable so if you run the command on the last answer it will revert back the fault current path it will not add the default which has been missed
The default path part of it is the default for any windows are:
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0
C:\Users$user\AppData\Local\Microsoft\WindowsApps
and the other depending on your apps like that
C:\WINDOWS\System32\OpenSSH
C:\Program Files (x86)\Microsoft Team Foundation Server 2010 Power Tools\Best Practices Analyzer
C:\Program Files\PuTTY
C:\Program Files\Docker\Docker\resources\bin
C:\ProgramData\DockerDesktop\version-bin
C:\Program Files\PowerShell\7-preview\preview
C:\Users$user\AppData\Local\Microsoft\WindowsApps
C:\Users$user\AppData\Local\Programs\Fiddler
C:\Users$user\AppData\Local\Microsoft\WindowsApps
C:\Program Files (x86)\OpenVPN\bin
C:\Users$user\AppData\Local\GitHubDesktop\bin
C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin
C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static
you can search what is the default PowerShell path for each program you have
To add a new path
$INCLUDE = "C:\tmp"
$OLDPATH = [System.Environment]::GetEnvironmentVariable('PATH','machine')
$NEWPATH = "$OLDPATH;$INCLUDE"
[Environment]::SetEnvironmentVariable("PATH", "$NEWPATH", "Machine")

Powershell can not call ImageMagick commands - IM Montage

I wrote a script in PowerShell and I am having various success calling Image Magick's montage.exe on different computers. The computer on which I wrote the script has no problem executing the 'montage' command, however on another computer with IM Installed the script errors out:
montage : The term 'montage' 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 \\Server\Contact_Sheet_Local.ps1:51 char:9
+ montage -verbose -label %t -pointsize 20 -background '#FFFFFF ...
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (montage:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have tried using montage.exe and even tried the entire path C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe. Additionally I tried setting the directory first:
Set-Location -Path 'C:\Program Files\ImageMagick-7.0.3-Q16'
montage...
Each time on a particular computer this fails. I have tried with IM versions 7.0.3-Q16 and 6.9.1-6-Q8 both x64 as both computers are x64
In the past, I have created scripts in .bat that use ImageMagick and I had to define the full path to the .exe as I mentioned above. But this doesn't seem to help in PowerShell.
Does anyone have any advice or experience with this problem?
If your path has spaces, it will fail if you're just trying to execute based on that. You'll want to utilize the dot operator
$Exe = 'C:\Program Files\ImageMagick-6.9.1-6-Q8\montage.exe'
If (-not (Test-Path -Path $Exe))
{
$Exe = 'C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe'
}
. $Exe -arg1 -etc
PowerShell does not execute programs in the current directory by default. If you want to run an executable that's sitting in the current directory, prefix the executable's name with .\ or ./. Example:
Set-Location "C:\Program Files\ImageMagick-7.0.3-Q16"
.\montage.exe ...
If you have an executable's name in a string or string variable and you want to execute it, you can do so using the & (call or invocation) operator:
& "C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe" ...
If you specify a path and filename that doesn't contain spaces, the & operator isn't required; example:
C:\ImageMagick\montage.exe ...
You could also write it this way:
& C:\ImageMagick\montage.exe ...
If you have an executable's filename in a string variable and you want to execute it, use &; example:
$execName = "C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe"
& $execName ...

Invoke-Sqlcmd cmdlet was not available. SQL Server PowerShell components may not be installed

I am getting the exception as mentioned when I am trying to execute sql queries using PowerShell
[ERROR] Invoke-Sqlcmd cmdlet was not available.
SQL Server PowerShell components may not be installed.
The weird thing I figured out was with this path $env:PSMODULEPATH. I am having the paths set as follows
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\;C:\Program Files\WindowsPowerShell\Modules\
Instead of the above if I just move the following path before SQL Server or to the first position it is working fine
C:\Program Files\WindowsPowerShell\Modules\
When I update this is how it looks
%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\WindowsPowerShell\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\
So is there any way to move it to the first place or above to the SQL PS module path
NOw you just need to set the environmental variable to point to that path externally:
Set-Item -Path env:psmodulepath -Value "C:\Program Files\WindowsPowerShell\Modules\;%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\;"
This should do our job.
Hope it helps.

Powershell Script parameters with path names fail invocation

I'm calling a PowerShell script from a BAT file:
powershell -nologo -file ./Run-Metrics.ps1 1 1 "RWSConsoleOnly" "batch" "RWSConsoleOnly" 3 "c:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FXCop\FXCopCmd.exe" "c:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets\" "14" "c:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop\Metrics.exe" "d:\a6\RWS_Test_Batch_XML\Microsoft_Build_XML\trunk"
and I'm getting this parse error:
Run-Metrics.ps1 : A
positional parameter cannot be found that accepts argument 'Studio'.
+ CategoryInfo : InvalidArgument: (:) [Run-Metrics.ps1], ParentCo
ntainsErrorRecordException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Run-Metrics.ps1
The parameters for the script are:
Param
(
# AHP Iteration
[Parameter(Mandatory=$false,Position=0)]
[int]$AHPIteration,
# buildAnalysisCount
[Parameter(Mandatory=$false,Position=1)]
[int]$buildAnalysisCount,
# buildAnalysisNames
[Parameter(Mandatory=$false,Position=2)]
[string]$buildAnalysisNames,
# buildAnalysisTypes
[Parameter(Mandatory=$false,Position=3)]
[string]$buildAnalysisTypes,
# buildAnalysisAsmbs
[Parameter(Mandatory=$false,Position=4)]
[string]$buildAnalysisAsmbs,
# FXCop Level
[Parameter(Mandatory=$false,Position=5)]
[int]$FXCopLevel,
# FXCopCmdPath
[Parameter(Mandatory=$false,Position=6)]
[string]$FXCopCmdPath,
# FXCop Ruleset Path
[Parameter(Mandatory=$false,Position=7)]
[string]$FXCopRulesetPath,
# Visual Studio Version
[Parameter(Mandatory=$false,Position=8)]
[string]$msvsversion,
# Metrics Command Path
[Parameter(Mandatory=$false,Position=9)]
[string]$metricsCommandPath,
# Working Path
[Parameter(Mandatory=$false,Position=10)]
[string]$workingPath
)
I don't understand why I'm getting this error. This was working two days ago (at least it was getting into the actual script...) I haven't changed the BAT file or the parameters, just code within the script. I've tried adding more quotes around the parameters that contain paths but that didn't help.
It turns out it was the path that ends in \Rule Sets\". The trailing backslash was escaping the closing quote.

How to properly set path in Powershell and 7zip?

I have a Powershell script to create a self-extracting archive via 7zip. But it's receiving this error:
cannot find specified SFX module
The Powershell code is:
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe
sz a -t7z -sfx -ppassword $fullpath $filetostore
Both variables are valid. I've tried -sfx and -sfx7z.sfx, same error. The 7z.sfx file is indeed in the correct folder with 7zip. I can also verify the alias is working, as the 7zip copyright appears when running the code (so 7zip commandline is being initiated). This command works outside Powershell.
I'm also tried Set-Location into the 7zip folder, but same error. What am I missing?
It seems you should add the 7-zip folder to your PATH environment variable to make things easier :
#find the 7-zip folder path
$7zPath = (Get-ChildItem "C:\Program Files","C:\Program Files (x86)" -Include "7-zip" -Recurse -ErrorAction SilentlyContinue).FullName
#add it to PATH environment variable
$env:Path += ";$7zPath;"
Then you can run 7z -sfx with no errors about the SFX module.
While Sodawillow has an answer that will work for the active session, a more permanent answer would be to add 7zip to the path for the Environment you are working in:
[Environment]::SetEnvironmentVariable("Path",$env:Path+";C:\Program Files\7-zip", [EnvironmentVariableTarget]::User)
The above one-liner should add 7zip to the active user account's path. Change 'User' to 'Machine' for the whole computer, or 'Process' for the currently running window. If you set 'User' or 'Machine', you will need to open a new powershell instance to see the change reflected.