Powershell PSModule unc path - powershell

I am adding a UNC path, like "\\server\share\modules" to the user session env variable PSModule like this:
$env:PSModulePath = $env:PSModulePath + ";\\server\share\modules"
But, when I try to load a module from this path, I get an error
PS C:\> Import-Module WS_XML_MODULE
Import-Module : The specified module 'WS_XML_MODULE' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-Module WS_XML_MODULE
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (WS_XML_MODULE:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Listing all the available modules, which should show the UNC path available modules, doesn't show me any of the UNC folder modules...
Get-Module -listavailable
Anybody knows why?
Thanks

Go this working...
I took a look at the system modules folder and the syntax of the folders and files are exacly the same, with dots separating words like Microsoft.Powershell.Something
I then changed the name of the PSM1 file to the same and got them into folder with the same name and BAmm.. in a heartbeat loaded all my modules.

Place the "WS_XML_MODULE.psm1 and WS_XML_MODULE.psd1" in the folder named "WS_XML_MODULE".
Make sure "WS_XML_MODULE" folder is located in "\\server\share\modules"
Note: Don't include the Module name in Path

Related

powershelll Mount-DiskImage "The system cannot find the path specified."

I have a batch file I'm executing on Windows 10 through the "Deployment and Imaging Tools Environment"
powershell Mount-DiskImage ./%WORKSPACE%/W10-LTSB.iso
The environment variable WORKSPACE has been checked and contains a valid path that exists as does the file W10-LTSB.iso, however when this command is executed it results in:
Mount-DiskImage: The system cannot find the path specified.
At line:1 char:1
+ Mount-DiskImage ./CA20-4002-OperatingSystem-AIMB-216/W10-LTSB.iso
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (MSFT_DiskImage:ROOT/Microsoft/.../MSFT_DiskImage) [Mount-DiskImage], CimException
+ FullyQualifiedErrorId : HRESULT 0x80070003,Mount-DiskImage
Everything looks valid and has been checked many times, what does this message mean?
When a path starts with a dot/period ., it refers to the current directory. When a shell session starts, its current directory is configuration dependent.
For example, try running a Powershell session. It should default in c:\Users\<username>. Run a Powershell as admin and it usually defaults in C:\WINDOWS\system32.
When mounting a disk image with a path beginning with a dot
powershell Mount-DiskImage ./%WORKSPACE%/W10-LTSB.iso
will tell Mount-DiskImage to look the file from its current directory's subdir. Should the current directory be unexpected, Powershell's looking the file from wrong a place.
As for a solution, use absolute paths, or make sure the file is located in a path that's available via current directory (whatever it is).

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

How to import splitpipeline module from a shared server?

My script uses the Powershell splitpipeline module, to bring parallel process and queues features.
The script and the module are stored on shared server, like \server\c$ , the idea is be able to run it from any computer.
Tried to put at begining of the script import-module \\server\c$\SplitPipeline but I recieve the error:
import-module : Could not load file or assembly 'file://\\server\c$\SplitPipeline\SplitPipeline.dll' or one of its dependencies.
Operation is not supported. (Exception from HRESULT: 0x80131515)
At D:\scripts\powershell\OstReport_BETA-PIPE.ps1:6 char:1
+ import-module \\server\c$\SplitPipeline
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], FileLoadException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand
If I try to copy it from the srv to the pc with:
Copy-Item -Path \\server\c$\SplitPipeline -Destination C:\windows\system32\WindowsPowerShell\v1.0\Modules -recurse -force
I get a access denied
any ideas¿?
thanks
The issue you're experiencing is that .NET assemblies can't be loaded from an untrusted UNC path, without special configuration. As you already discovered, the simplest solution is to copy the module to your local computer first.
To work around the "Access Denied" message, copy the module to your user directory, not the system-wide directory.
c:\users\<username>\Documents\WindowsPowerShell\Modules
You need to run PowerShell "as Administrator" in order to have permission to copy to the system folder, however it is generally recommended not to modify the default system directory. Instead, copy the module to your user folder (as above).

How to change to temp directory in PowerShell?

In PowerShell I can echo the value of %TEMP% using the command $Env:TEMP. Here is the output on my machine:
PS> $Env:temp
C:\Users\IAIN~1.COR\AppData\Local\Temp
When I try to change to the directory using the cd command, I receive this error:
PS> cd $Env:temp
Set-Location : An object at the specified path C:\Users\IAIN~1.COR does not exist.
At line:1 char:3
+ cd <<<< $Env:temp
+ CategoryInfo : InvalidArgument: (:) [Set-Location], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.SetLocationCommand
I suspect that PowerShell is interpreting the 8.3 file name literally. The long file name of the directory is C:\Users\iain.CORP\AppData\Local\Temp. When I try cd C:\Users\Iain.CORP\AppData\Local\Temp, the directory changes successfully.
How can I open the path in $Env:TEMP using PowerShell? Do I have to have the long file name first?
You don't need to access the %TEMP% environment variable directly.
.NET provides a GetTempPath method as a more general solution.
$TempDir = [System.IO.Path]::GetTempPath()
cd $TempDir
On my machine, this changes to the directory C:\Users\Iain.CORP\AppData\Local\Temp.
Remarks from the documentation:
This method checks for the existence of environment variables in the
following order and uses the first path found:
The path specified by the TMP environment variable.
The path specified by the TEMP environment variable.
The path specified by the USERPROFILE environment variable.
The Windows directory.
Thanks to Joe Angley for sharing the technique.
Open $env:temp by first resolving its fullname like this:
cd (gi $env:temp).fullname
cd $env:temp
This worked for me.

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 + ";."