Running a PowerShell Script .PS1 - powershell

I am trying to generate a MachineKey for my application using the PowerShell script found in kb2915218.
I have copied the function into notepad and saved as a .PS1 file. Now if I look at this file through explorer it is being recognised as a PowerShell file.
I then have run PowerShell and CD to the directory of my .PS1 file.
I then ran the following command:
Set-ExecutionPolicy Unrestricted
followed by:
.\Powershell-Generate-MachineKey.ps1
(the name of my script). And finally I then tried running the command
Generate-MachineKey
However I get the message:
Generate-MachineKey : The term 'Generate-MachineKey' 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
+ Generate-MachineKey
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Generate-MachineKey:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Can someone please tell me where I am going wrong here?

The script just defines a function, so if you execute it like this:
.\Powershell-Generate-MachineKey.ps1
it won't do anything, because the function isn't invoked anywhere and also isn't made available in the current context. For the latter you need to dot-source the script
. .\Powershell-Generate-MachineKey.ps1
The dot-operator basically executes the script in the current context instead of a child context. That way the definitions from the script become available in the current context, and you can invoke the function like this:
Generate-MachineKey

Related

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

I have downloaded CPDF because I have to batch work on a large number on PDFs.
The executable is on my Desktop: C:\Users\admin\Desktop\cpdf.exe
I am running PowerShell ISE on Windows 7 as Administrator. I have set Set-ExecutionPolicy Unrestricted.
My prompt is at the desktop location: PS C:\Users\ftsadmin\Desktop> but if I try to run cpdf: PS C:\Users\ftsadmin\Desktop> cpdf or as PS C:\Users\ftsadmin\Desktop> cpdf.exe, I get the following error:
The term 'cpdf.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 line:1 char:9
+ cpdf.exe <<<<
+ CategoryInfo : ObjectNotFound: (cpdf.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I don't understand. When I am doing exactly the same in my Windows XP VM, it works (but I prefer Windows 7+ because of the PowerShell ISE).
Any ideas what I'm missing?
Unlike CMD PowerShell does not automatically include the current directory in the search path (it behaves like Unix shells in this respect). To run a program or script from the current directory you need to use the relative or absolute path:
.\cpdf.exe
C:\Users\ftsadmin\Desktop\cpdf.exe
The execution policy has nothing to do with this, as it governs only the execution of PowerShell scripts, not of external commands.

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.

PowerShell issue - I have to type ./ to run bat file

I've just installed PHP & Yii Framework. It works fine, I played with CMD. But after a while I switched to PowerShell ISE. I navigated to Yii folder:
cd C:\dev\yii-1.1.9.r3527\framework
and I issued command:
yiic.bat
and I get an error:
PS C:\dev\yii-1.1.9.r3527\framework> yiic.bat
The term 'yiic.bat' 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:9
+ yiic.bat <<<<
+ CategoryInfo : ObjectNotFound: (yiic.bat:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
However when I type:
./yiic.bat
into PowerShell window, it works fine.
Is there a way to aviod typing ./ every time I run a bat file?
The framework directory you're trying to run the batch file from is evidently not in your path. When you type yiic.bat into the shell, it looks for that file in the list of directories contained in your path environment variable. See this question for information about how to set your path in powershell.
For example, if you want to be able to run batch files in the C:\dev\yii-1.1.9.r3527\framework directory, you can say $env:Path = $env:Path + ";C:\dev\yii-1.1.9.r3527\framework".
Or, as mloskot says, you can just add the current directory to your path, though that can pose a minor security risk. See e.g. this question for a bit of discussion on that.
this is an old question, but I stumbled on it looking for something else.
Powershell requires you to type .\ to run commands in the current by design.
https://blogs.technet.microsoft.com/csps/2010/06/06/introduction-to-windows-powershell-scripting/
I don't have PowerShell to try this out, but if you set the path to include current folder, this should work:
$env:Path = $env:Path + ";."

Appfabric Powershell error "The term 'Get-CacheStatistics' is not recognized as the name of a cmdlet.."

I'm calling the simple following Powershell AppFabric command:
powershell -noexit c:\scripts\ApplyClusterConfig.ps1
and the script just contains:
Get-CacheStatistics
and I'm getting the following error:
The term 'Get-CacheStatistics' is not recognized as the name of a cmdlet, funct
ion, 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:\scripts\ApplyClusterConfig.ps1:1 char:20
+ Get-CacheStatistics <<<<
+ CategoryInfo : ObjectNotFound: (Get-CacheStatistics:String) [],
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I've done everyting that I can find on the web including
Set-ExecutionPolicy Unrestricted
and using the .\ to refer to the file:
powershell -noexit .\ApplyClusterConfig.ps1
and setting the environment path to include c:\Scripts
But the error keeps persisting. Can anyone help please because I've run out of all google options. Thanks.
As the errors says, it is not able to find Get-CacheStatistics as a cmdlet, function, script file, or operable program. You need to laod the necessary modules.
Look here for guidance on how to load the necessary modules to run AppFabric cmdlets: http://msdn.microsoft.com/en-us/library/ee677295.aspx
You will probably have to add one or more of the following imports ( maybe in your script):
Import-Module ApplicationServer
Import-Module distributedcacheconfiguration
Import-Module distributedcacheadministration
Execution policy has nothing to do with this error and you are running the script fine.

Running a function in PowerShell

I am new to PS and have been given a script to run. The first thing I do is type in c:\powershell\ir.ps1. This seems to work. Then after defining my clients directory I am supposed to be able to just type in functions such as ir-n. This worked at the person's desk that showed me how to do it, but I get the following error:
The term 'ir-n' 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:5
+ ir-n <<<<
+ CategoryInfo : ObjectNotFound: (ir-n:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Is there something simple I can do to get it to run? I see the function in the ir.ps1 file so I know it is there.
It looks like you are running the ir.ps1 script when you should be sourcing it. I'm guessing that the ir.ps1 file is defining a function named ir-n. In that case running the script will not define the function in the script's context but not the command window. You need to source the script to get it to persist in the command window.
Try running the following
PS$> . c:\powershell\ir.ps1
After running this then try ir-n.
You probably need to dot source the script which will leave the functions it defines available in the global scope e.g.:
PS> . c:\powershell\ir.ps1