Unable to import a powershell module from a script - powershell

I have a strange problem I have created a module and placed it in the “C:\Windows\System32\WindowsPowerShell\v1.0\Modules” directory .
I can open powershell and import my module fine but it does not work when I do the following.
I have batch file that calls the powershell script using the following command
START /min /wait powershell "C:\test.ps1" "%1"
The script starts but it is unable to import module.
I placed this command “Get-Module -ListAvailable | Out-File c:\module.txt” in the test.ps1.
When I review the module.txt file I see it does not list my module there.
It seems that for some reason when I call the script this way it can’t access the module

A module has to be either a .psm1 file or a .psd1 manifest file (or a dll). A .ps1 doesn't qualify as a module, it is "just" a script.

Related

How to convert PowerShell code for a batch file?

This code unblocks a OneDrive library, for the sake of security. The library is found in the C:\ODTool directory, and is downloaded form a GitHub online open project. It uses PowerShell to import a module, then run the command Get-ODStatus, to determine the sync status of OneDrive, outputting it to output.csv in the current directory.
PS C:\ODTool> Unblock-File -Path C:\ODTool\OneDriveLib.dll
Import-Module C:\ODTool\OneDriveLib.dll
Get-ODStatus > output.csv 2>&1
I need to convert this PowerShell script to an executable batch file command.
Create a txt file and add the following code:
#ECHO OFF
Powershell.exe -executionpolicy bypass -File <full path to ps1>
then save the file as a .bat

Why is import-module or dot sourcing a file not working in powershell scripts?

I am developing and running PS scripts in VS Code using the PowerShell Extension. I have defined a number of functions in a separate PS module file, which I have saved here:
C:\Users\MyName\Documents\PowerShell\Modules\PowerBiFunctions\PowerBiFunctions.psm1
But when attempting to import the module with
Import-Module PowerBiFunctions
or a copy placed in the same directory as the script
Import-Module $PSScriptRoot\PowerBiFunctions.psm1
I get the following error:
Import-Module : The specified module 'PowerBiFunctions' was not loaded because no valid module file was found in any module directory.
In addition, I have made a copy of the file with the .ps1 extension and placed in the same directory. However, I can't seem to include it with simple dot sourcing:
.\PowerBiFunctions.ps1
or
. .\PowerBiFunctions.ps1
What seems to be the problem?
Create a module manifest:
# cd to module folder
Set-Location C:\Users\MyName\Documents\PowerShell\Modules\PowerBiFunctions\
# create new manifest file
New-ModuleManifest .\PowerBiFunctions.psd1 -RootModule .\PowerBiFunctions.psm1 -FunctionsToExport list,of,exported,function,names
Now you can import it by name:
Import-Module PowerBiFunctions

PowerShell script path from module

I have this myModule.psm1 in modules directory:
function my-test{
$global:PSCommandPath>C:\test.txt;C:\test.txt
}
and this myScript.ps1
my-test
When I run myScript.ps1 with PowerShell ISE, I got path of myScript.ps1 in test.txt file, as i expect.
When I run myScript.ps1 with PowerShell, I got empty test.txt file.
How can I fix it?
This is PowerShell level 100 stuff.
If this is really a module, .psm1 file, then it should be in your modules directory (or directly called from a directory path) and be properly named, then loaded into memory using...
Import-Module -Name 'YourModuleName'
If you are trying to use a function from a .ps1 file, it too must be loaded, by a direct run of the script or dot-sourcing it.
. .\my-test.ps1
At this point, you call any of the functions in the .ps* loaded resource. Your function name and module name should be different things to avoid confusion.
See the help files, web articles, Youtube videos regarding loading .ps* file and running functions from .ps* files. I've provided the link above to Youtube.

Post Build PowerShell Script does not include installed modules

I am calling the below script in my post build configurations:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoProfile
-ExecutionPolicy RemoteSigned -file "\Sclddev8\tfs\Scripts\slack-notice.ps1" -Verb RunAs;
However, I keep getting this error:
Send-SlackMessage : The term 'Send-SlackMessage' is not recognized as the name
But I have installed this module in my environment and if I open a PowerShell console or run the file outside of this build process, works without issue.
When you install a Powershell module, you are technically importing the module from your profile every time you open a new Powershell window. By running Powershell with the "-NoProfile" switch, you're preventing the module from being imported (even though it's "installed" and the files are present).
What may be your best option, if you want to keep the "-NoProfile" switch active, is to have a line at the top of your script to import the module before continuing. If you're using Warren Frame's "PSSlack" module, the command you need is:
> Import-Module PSSlack
I hit the same issue.
What helped was... copying the folder into C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules.
Yup, it makes a difference.

Execute any file as powershell script

I encountered a challenge that I failed to resolve the way I wanted it to do.
I got a file that contains a powershell script, but that file does not have the extension assigned to powershell. The question is: How can I execute a powershell in a script file with the wrong file extension (or none)?
Invoke-Expression does not seem to work because it always executes the default action assigned to the file type. If I give that cmdlet a *.txt file the editor pops open.
I know that I can resolve that by renaming the script file or naming it properly in the first place. This is what I ended up doing.
Still I wonder if it is possible to execute a file as a script with the wrong file extension without modifying, renaming or coping the file. And if it is not working… why is that?
Powershell is designed such that executing or dot sourcing a file requires a .ps1 extension, and Powershell.exe will refuse to run any file that doesn't have that extension.
One way to invoke Powershell code from a non-ps1 file is to launch Powershell.exe using STDIN, and pipe your script to it. This requires a new shell, so is not very good for launching scripts from within an existing scripting environment.
Powershell.exe - < thescript.txt
Another way is to create a temporary .ps1 file and execute that. This has the advantage of using the current scripting environment, but requires a temporary file.
Copy-Item -Path '.\thescript.txt' -Dest '.\temp.ps1'
. .\temp.ps1
del .\temp.ps1
In my opinion, the file extension restriction is silly, but that's how it was designed. Apocryphally, this is for security reasons, but I can find no citation to back it up.
Or you use Get-Content to read the file and then invoke that with Invoke-Expression or Invoke-Command -ScriptBlock.