The term 'X' is not recognized as the name of a cmdlet - powershell

I found this script that will loop through .csv files and combine them into one Excel worksheet. I then created a second script to call this one with the following:
echo "Combining .csv files into Excel workbook"
C:\PowerShell\ConvertCSVtoExcel.ps1
Get-ChildItem *.csv | ConvertCSV-ToExcel -output ePortalMontlyReport.xlsx
echo " "
But when I try to run the script, I am getting the following error:
ConvertCSV-ToExcel : The term 'ConvertCSV-ToExcel' 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:\PowerShell\Merge.ps1:3 char:23
+ Get-ChildItem *.csv | ConvertCSV-ToExcel -output ePortalMontlyReport. ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (ConvertCSV-ToExcel:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any suggestions on how to resolve this?

You need to dot-source the .ps1 file to get the functions in it imported into your session.
. C:\PowerShell\ConvertCSVtoExcel.ps1

Related

PowerShell The term 'which' is not recognized as cmdlet function script file or operable program

I am using PowerShell and want to do a which command, however I am seeing this error:
which : The term 'which' 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
+ which xxxxxx
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (which:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I have done a search on Google and there is no specific answer to my question. Has anyone encountered the same error before and how can I fix it?
Putting an answer together from the comments:
which is a standard command of Linux, but is not normally present on Windows.
The equivalent command is Get-Command, or if you have it, you can run where.exe.

No PowerShell help in Docker using WindowServerCore

I'm using Docker and WindowsServerCore base image to serve different PowerShell modules to some Ops. If I start the container and try to use the Get-Help cmdlet I get the following error:
PS C:\Users\ContainerAdministrator> help *service*
more.com : The term 'more.com' 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:9 char:19
+ } else { $input | more.com }
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (more.com:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any hints?
Thx
From these two questions 1 and 2 state that help doesn't run as Get-Help as many commands do when run without Get- but is a function that pipes to more.com. Likely the executable more.com isn't included in the WindowsServerCore base image.

Error when using New-Item / md / mkdir

I've currently got a script that:
Loops through some files
Checks the name of the file
If there is no directory for the file it will create one
Moves the file into the directory
The moving logic works fine. However if a directory does not exist I am given this error (the path is valid, except it does not exist)
C:\Users\User\Documents\Directory\FileName : The term 'C:\Users\User\Documents\
Directory\FileName' 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
C:\Users\User\Documents\Directory\FileName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\User\Documents\Directory\FileName) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The curious part is that it does actually create the folder - however it crashes my script.
Here's the problem part of the script
function CanCreate($dir) {
return !(Test-Path $dir)
}
if (CanCreate($fullDestinationPath)) {
New-Item $fullDestinationPath -ItemType Directory
}
md/mkdir behave differently to New-Item in that they do crash the script, however New-Item prints the error and continues (script seems to finish its job).
Edit:
The issue seems to stem from the fact that I am calling the script from another script.
$ScriptPath = "C:\Powershell Scripts\script.ps1"
& $ScriptPath | Invoke-Expression
The issue was with the fact that I was using | Invoke-Expression to trigger the script.
Simply calling & $ScriptPath, omitting the | Invoke-Expression, was enough to trigger the script.
Thanks everyone.

Azure Function in PowerShell to untar file

I am trying to convert some a number of files compressed into a .tar.gz archive into a single file. To do so I need to first untar the file.
I have a copy of 7z.exe loaded into the directory, and calling the command locally works
.\7z.exe x *.tar.gz
I've uploaded the exe into the wwwroot/poshUntar directory alongside the run.ps1 file that gets executed, and using the online editor I can execute the powershell script. I would of course expect my function to fail generally since I'm not providing the variable values but I would not expect it to error about finding the 7z.exe file
.\7z.exe : The term '.\7z.exe' 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 D:\home\site\wwwroot\poshUntar\run.ps1:10 char:1
+ .\7z.exe x *.tar -o logs
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\7z.exe:String) [], CommandNot
FoundException
+ FullyQualifiedErrorId : CommandNotFoundException
What is the correct way to invoke an executable inside a PowerShell Azure Function?
I believe that you also need to upload 7z.dll into the wwwroot directory.
Could you try using the following code-segment in your script?
Set-Location D:\home\site\wwwroot\poshUntar
.\7z.exe x *.tar.gz
.\7z.exe x *.tar -ologs

Open program files directory using powershell and env variable

My goal is to create a PowerShell script, which would open specific directories in Windows Explorer.
Some of the directories could be referenced through the environment-variables.
However I got a problem with the following command
ii %programfiles(x86)%
Execution returns the following error:
The term 'x86\' 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:23
+ ii %programfiles\(x86\ <<<< )%
+ CategoryInfo : ObjectNotFound: (x86\:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Can you kindly explain to me, what I'm doing incorrectly here?
%variable% is batch notation. In PowerShell you have to use $env: to access environment variables.
Invoke-Item ${env:ProgramFiles(x86)}
The curly brackets are required, because without them the parentheses wouldn't be recognized as part of the variable name.