1

I have a strange experience using New-Itemin powershell. My SMB port are all blocked on firewall, so my only alternative is using a PSSession. I successfully create a session and can invoke commands over it.

Sometimes opening a session and using the command from local host works and it uses the 5986 port to connect to the remote server. I checked it on the firewall.

New-Item -Path "\\remoteServer\c$\Temp\1" -Type Directory -Verbose

Other times the same command want to use 445 and get blocked by the firewall.

The powershell script get executed by Jenkins if it makes any difference. So my environment stays the same.

The same applies to the command Copy-Item. Copy-Item at least has a switch -ToSession that I can use to always use the opened session and it will always gets through without 445 port. New-Item does not have this switch.

What could be the reason for such behavior? Is there any way I can force powershell to use the created session for New-Item?

4
  • PowerShell cannot supplant or subvert firewall settings. New-Item is just a cmdlet, sessions are connections and though they can be used in code blocks, they have nothing to do with each other. They are completely separate actions.
    – postanote
    Apr 11 '20 at 1:33
  • The tell me how I sometimes can create items on remote computer and other times not. No FW changes, no script changes. Unc/samba port are always blocked on FW, but I suspect the New-Item is sometimes using the opened session over 5986.
    – RokX
    Apr 14 '20 at 8:29
  • Doing things on a remote target via Powershell requires that PowerShell remoting be enabled and you are either an admin on that target (not there are a few cmdlets you an use that doesn't require remoting) and PowerShell remoting requires port 5986 by design and this is well documented. docs.microsoft.com/en-us/archive/blogs/christwe/…, cmdlets don't add/remote ports, the know nothing about them or FW rules, the PowerShell session is. Sometimes, means an environmet issue, that needs to be looked at. Losing, WinRM, Session Security, etc.
    – postanote
    Apr 14 '20 at 20:21
  • Maybe I wrote the question in a confusing way, but you missed the whole point of the question. Powershell remoting is established and working, I can use it without issues. I get strange behavior with 'New-Item -Path "\\remoteServer\c$\Temp\1"' when using unc paths. Sometimes it works, other times it does not work. Ports for unc access are all always blocked. I have admin rights on both machines.
    – RokX
    Apr 15 '20 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.