New-Item Behavior Changes When Using Runspace to Run Command Remotely - vb.net

I am using VB.Net to open a Powershell runspace using WSManConnectionInfo. The connection works fine. I am trying to run a New-Item command in order to create a new virtual directory.
New-Item "IIS:\Sites\ExternalInventory FTP\TestVT" -Type VirtualDirectory -physicalPath "C:\"
The command works fine when running it locally on the server. However, when ran remotely through the runspace I get an error:
A parameter cannot be found that matches parameter name 'physicalPath'.
I initially tried to use use the New-WebVirtualDirectory command to create the virtual directory, but this led to other errors. I need to use New-Item.
What is causing the difference in behavior of the New-Item command, and how can I use it to create a new virtual directory through the runspace?
Note: I have tried other commands through the runspace and they seem to work as expected.
EDITS
I know that the New-Item command does not have the parameter "physicalPath" listed in the docs: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-6
The New-Item command is used in multiple places in the docs to create virtual directory with the physicalPath parameter. See links
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-6
https://forums.iis.net/t/1223546.aspx?New+WebVirtualDirectory+does+not+work+when+invoked+remotely+and+a+UNC+path+is+used

As pointed out in the comments, I needed to include the WebAdministration module in my script.
Thanks TheMadTechnitian

Related

Trying to execute script through tclsh + sftp

For context i am trying to run a script that is hosted on a server made with TCL.
To run this script i log in a Cisco router and use the command:
tclsh tftp://[server_ip]/location/script.tcl
and it works over tftp, but when i try with sftp it does not work, says "No such file or directory", even tho i can copy the file and that works.
the command i use to cope the file is
copy sftp: flash:[server_ip]/location/script.tcl
so the router supports sftp.
and to try to run it i do exactly like tftp:
tclsh sftp://[server_ip]/location/script.tcl
to no avail.
So the copy works but executing the script does not work.
Any idea on how to fix this?
EDIT: I also tried SCP to no avail

Different behavior when launching with -file parameter

This all feels like a bug, but I hope someone can prove me wrong.
PS version: 5.1.17763.592
I'm launching a powershell script from a UNC path with the "-file" and "-NoExit" parameter.
This is done from a VB.net application. The VB.Net .exe is run as admin, the Powershell session also then starts as admin.
Dim script_path As String = "-NoExit -ExecutionPolicy RemoteSigned -file \\PATH\Test.ps1"
Process.Start("Powershell", script_path)
This all launches fine, but for some unknown reason this Powershell instance do not behave the same way as if I were to run powershell as admin directly.
For example:
Some registry information cant be found or is returned differently.
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Measure-Object
Returns 116 in "-file" instance, 36 in standard instance.
cd "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\Execution History\"
Returns not found in -file instance, but it do exist. I have found a few registry keys like this one.
This instance has 13 fewer modules avaliable (Get-Module -ListAvailable | Measure-Object), some cmdlets cant be run, for example Get-LocalUser.
It kind of feels like im remoting to a completely different computer when I'm not.
Same computer, same user, both powershell run as admin.
I've tried:
Launch from a local directory (Not UNC)
Remove -NoExit param.
Launching cmd as admin manually then powershell.exe -NoExit -ExecutionPolicy RemoteSigned -file \\UNC\Test.ps1 (This do not have the same limited instance)
I would understand if this mode was somewhat limited and modules need to be imported manually, but it doesnt explain why registry information is returned differently.

SSH into Linux Subsystem, not Windows CMD

I assume I messed up something since nobody has a similar question here. When I SSH into a windows laptop it does not give me the UNIX shell by default, instead it lands me directly at the Windows prompt. I then have to enter bash, followed by cd and then I can run the commands I need.
Is this a windows setting problem or an openssh issue and does anyone have any suggestions on how I can avoid needing to enter these commands every time that I log in?
The Windows SSH daemon is openssh, and by default it is setup to use cmd as the shell, but you can edit the configuration to use powershell, bash, or any other third party shell. Follow the information in this link.
Its given in MS PoweShell documentation should go through.
To set the default command shell, first confirm that the OpenSSH installation folder is on the system path. For Windows, the default installation folder is SystemDrive:WindowsDirectory\System32\openssh.
Configuring the default ssh shell is done in the Windows registry by adding the full path to the shell executable to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH in the string value DefaultShell.
Here is the example, the following Powershell command sets the default shell to be PowerShell.exe:
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
For further explanation refre this link.

Microsoft.Azure.IpSecurityRestriction not found in Azure PowerShell script

I am trying to run some PowerShell commands and my script is failing on the following line:
$ipsr = New-Object Microsoft.Azure.IpSecurityRestriction
The error is:
Cannot find type [Microsoft.Azure.IpSecurityRestriction]: verify that the assembly containing this type is loaded
I am trying to run this “inline” in an Azure PowerShell task as part of my deployment pipeline. Is this supported or do I need to first import an assembly?
I can reproduce your issue. First, it should be Microsoft.Azure.Management.WebSites.Models.IpSecurityRestriction, not Microsoft.Azure.IpSecurityRestriction, then make sure you have installed the Az.Websites powershell module, just use the command below.
Import-Module -Name Az.Websites
New-Object Microsoft.Azure.Management.WebSites.Models.IpSecurityRestriction
Besides, actually we import the module just for the Microsoft.Azure.Management.Websites.dll, so you can also use the command as below, check the path of your .dll file.
Add-Type -Path 'C:\Program Files\WindowsPowerShell\Modules\Az.Websites\1.1.0\Microsoft.Azure.Management.Websites.dll'
New-Object Microsoft.Azure.Management.WebSites.Models.IpSecurityRestriction

ConEmu + WSL: Open new console in current tab directory

I'm using WSL and ConEmu build 180506. I'm trying to setup a task in ConEmu to use the current directory of the active tab when opening a new console but I cannot get it to work.
What I did is to setup the task {Bash: bash} using the instructions on this page
setting the task command as :
set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -C~ -cur_console:pm:/mnt
Then following the instruction on this page, I added to my .bashrc
if [[ -n "${ConEmuPID}" ]]; then
PS1="$PS1\[\e]9;9;\"\w\"\007\e]9;12\007\]"
fi
and finally setup a shortcut using the macro :
Shell("new_console", "{bash}", "", "%CD%")
But it always open the new console in the default directory ('/home/[username]').
I don't understand what I'm not doing right.
I also noticed that a lot of environment variables listed here are not set. Basically, only $ConEmuPID and $ConEmuBuild seem to be set.
Any help would be appreciated.
GuiMacro Shell was intended to run certain commands, not tasks.
You think you may try to run macro Task("{bash}","%CD%")
Or set your {bash} task parameters to -dir %CD% and just set hotkey for your task.
Of course both methods require working CD acquisition from shell. Seems like it's OK in your case - %d shows proper folder.
I found the answer:
Shell("new_console:I", "bash.exe", "", "%CD%")
The readme is actually pretty good: https://github.com/cmderdev/cmder/blob/master/README.md