VS Code - 'mcs' is not recognized as the name of a cmdlet - visual-studio-code

When i tried to run the program for the first time, it crashes and this is what i got. im sure that i've installed mono sdk
PS D:\Microsoft VS Code\Projects\New Projects> cd "d:\Microsoft VS Code\Projects\New Projects\" ; if ($?) { mcs Program.cs -out:Program } ; if ($?) { mono Program }
mcs : The term 'mcs' 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:62
+ ... "d:\Microsoft VS Code\Projects\New Projects\" ; if ($?) { mcs Program ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (mcs:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException**strong text**

Related

Parsing a web page for text

I'm trying to search a webpage for a word "Green". It tells me things are fine but if it finds "Red" it tells me things are bad. What I've done below is very simple but I'm thinking its enough to ask the question.
$web = Invoke-WebRequest http://myip/vrsinfo/aghealth/DISTAGAR
$web.ToString() -split "[`r`n]" | Select-String "Green"
Here is what it returns
name="agSolarwindsHealth" value=Green readonly>
Here is what I've tried so far
if (name="agSolarwindsHealth" value=Green readonly>) {
'This number is 1'
} else {
'This number is not 1'
}
Error
name=agSolarwindsHealth : The term 'name=agSolarwindsHealth' 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:3 char:5
+ if (name="agSolarwindsHealth" value=Green readonly>)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (name=agSolarwindsHealth:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

how to execute an exe with variable value for path and arguments

I'm trying to execute in my powershell script the command below :
D:\Apps\Documentum\product\7.3\bin\idql.exe -udmadmin -pPassword dctm04 -RC:\temp\documentum\session_list.txt -w20 > C:\temp\documentum\session_logstash.txt
In my powershell script I do that:
$DOCBASE="dctm04"
$USER_DOCBASE="dmadmin"
$USER_PWD="Password01"
$IDQL_PATH="D:\Apps\Documentum\product\7.3\bin"
$QRY_SESSIONS="C:\temp\documentum\session_list.txt"
$QRY_LOG_SESSIONS="C:\temp\documentum\session_logstash.txt"
$IDQL_PATH\idql.exe -u$USER_DOCBASE -p$USER_PWD $DOCBASE -R$QRY_SESSIONS -w20 > $QRY_LOG_SESSIONS
But it doesn't work properly, I receive the error below :
At C:\temp\documentum\Generate.ps1:49 char:13
+ $IDQL_PATH\idql.exe -u$USER_DOCBASE -p$USER_PWD $DOCBASE -R$QRY_SESSIONS -w20 ...
+ ~~~~~~~~~
Unexpected token '\idql.exe' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
I think, i don't use variable properly on my command.
Please note my powershell version is :
PS C:\temp\documentum> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
could you give me the solution in order to solve my problem
The reason is that combining a string to executable name makes no sense to Powershell's parsing rules. Use the call operator & or Invoke-Item. Like so,
$ssms="C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio"
PS C:\> $ssms\ssms.exe
At line:1 char:6
+ $ssms\ssms.exe
+ ~~~~~~~~~
Unexpected token '\ssms.exe' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
C:\>& $ssms\ssms.exe
# Launches SSMS succesfully
C:\>Invoke-Item $ssms\ssms.exe
# Launches SSMS succesfully
There's nice a document about running executables.

PowerShell Native Cmdlet Set-Alias with Function and Switch

Question:
Is it possible to set an alias that calls both a function and a switch?
Here is an example I am working with.
Set-Alias -Name myidea -Value 'Test-FunctionIdea -SwitchIdea'
function Test-FunctionIdea {
param(
[switch]$SwitchIdea
)
if($SwitchIdea)
{
Write-Host 'Good Idea'
}
}
Here is the error I am having:
myidea : The term 'Test-FunctionIdea -SwitchIdea' 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
+ myidea
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (Test-FunctionIdea -SwitchIdea:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
If it is possible please let me know how it's done. I've searched the internet for this and came up with no results.
You can check $MyInvocation.InvocationName and adjust the parameters inside the function:
function Some-Function([switch] $foo) {
if ($MyInvocation.InvocationName -eq 'foo') { $foo = $true }
}
Set-Alias foo Some-Function
The [usually negligible] advantage is that it doesn't create an additional execution context for the new function scope.

Catching errors from external command

If I running this function (with out try..catch):
executezipFullBackup -PathFileLocation "Z:\" -PathSaveBackup "E:\" -NameOfBackup "MyNASStorage"
PoweShell give me this error:
sz :
At C:\PowerShellF\zipFunction.ps1:6 char:9
+ sz a -t7z "$PathSaveBackup\$NameOfBackup" "$PathFileLocation" #backup
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
ERROR:
Duplicate filename on disk:
BackupTest\private\103.tmp\usr\bin\mail
BackupTest\private\103.tmp\usr\bin\Mail
And that error is OK for me, because I know what is wrong on this step.
But my problem is, if I put this script in try..catch, I dont get this error.
Example:
$ErrorActionPreference = "Stop"; #this is for Try Catch
try {
zipFullBackup -PathFileLocation "$PathFileLocation" -PathSaveBackup "$PathSaveBackup" -NameOfBackup "$NameOfBackup"
} catch {
$MyError = ($Error[0] | Out-String);
Write-Host "$MyError"
}
I get error like this:
sz :
At C:\PowerShellF\zipFunction.ps1:6 char:9
+ sz a -t7z "$PathSaveBackup\$NameOfBackup" "$PathFileLocation" #backup
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
This error is not enough for me, because I don't see ERROR! I don't see this:
ERROR:
Duplicate filename on disk:
BackupTest\private\103.tmp\usr\bin\mail
BackupTest\private\103.tmp\usr\bin\Mail
How can I get try..catch to display this error?
That error message is not part of a PowerShell error/exception, but a message 7zip itself prints to STDOUT. Therefore it can't be caught with a try..catch block, not even if you set $ErrorActionPreference = 'Stop'.
What you can do is collect the command output in a variable, though:
$msg = zipFullBackup ...
The solution for your question is to add the 7zip option -bse1
& cmd.exe /c 'C:\"Program Files"\7-Zip\7z.exe a "c:\DestinationFolder\NameOnZipFiles" c:\FolderToZip\* -bse1
But a better solution for this is to take out the 7zip %errorlevel% from batch, because 7zip is not a PowerShell command.
To do so, use the following code in powershell. Yes the errorlevel variable needs to be written with !errorlevel!
[int]$7zipError=& cmd.exe /v /c 'C:\"Program Files"\7-Zip\7z.exe a -mx0 "c:\DestinationFolder\NameOnZipFiles" c:\FolderToZip\* -bso0 -bsp0 -bse0 &echo:!errorlevel!'
if ($7zipError -gt 0)
{
Write-Host "Errorlevel: $($errorlevel)"
#0 No error
#1 Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed.
#2 Fatal error
#7 Command line error
#8 Not enough memory for operation
#255 User stopped the process
}
If you want to know more and why you need to use “cmd.exe /v” I have written more about this in
PowerShell, only "> $out" catch full error of failed 7Zip extract, why?

Powershell script with params *and* functions

I want to write a powershell script that takes in params and uses functions.
I tried this:
param
(
$arg
)
Func $arg;
function Func($arg)
{
Write-Output $arg;
}
but I got this:
The term 'Func' 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 func.ps1:6 char:5
+ Func <<<< $arg;
+ CategoryInfo : ObjectNotFound: (Func:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Fine, I thought. I'll try this instead:
function Func($arg)
{
Write-Output $arg;
}
param
(
$arg
)
Func $arg;
But then, I got this:
The term 'param' 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 C:\Users\akina\Documents\Work\ADDC\func.ps1:7 char:10
+ param <<<<
+ CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Is what I'm asking for doable? Or am I being unreasonable in my request?
The param block in a script has to be the first non-comment code. After that, you need to define the function before you invoke it e.g.:
param
(
$arg
)
function Func($arg)
{
$arg
}
Func $arg
The Write-Output is unnecessary in your example since the default behavior is to output objects to the output stream.
All you need ius to make sure PARAM is first string of your script.
You can put the param tag inside the function..
Something like this:
function func($avg)
{
param
(
$avg
)
}