Python Forum
using PowerShell from Python script for mounting shares
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using PowerShell from Python script for mounting shares
#6
You need to be careful about "\" in string literals because backslash before some characters is the start of an escape sequence, while in front of other characters is just a backslash. "\\" is a single backslash. "\c" is not an escape sequence, but I had to look it up. When using backslashes in a string, as a backslash, use raw string to be safe.

I don't know if any of the network drive parts are correct, but this is how I would write.
script_file = 'C:/Scripts/somescript/Connect-0.ps1'
with open(script_file, 'w') as file :
    file.write(
        '$net = new-object -ComObject WScript.Network\n'
        fr'$net.MapNetworkDrive("Z:", "\\{host_name}\c$", $false, "domain\USER", "SOMEPASS")\n'
    )
print(subprocess.call(script_file))
Reply


Messages In This Thread
RE: using PowerShell from Python script for mounting shares - by deanhystad - Mar-10-2024, 04:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing web form to powershell robertkwild 0 42 6 hours ago
Last Post: robertkwild
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,523 Jun-29-2023, 11:57 AM
Last Post: gologica
  PowerShell & Python deep_logic 2 787 Jun-06-2023, 06:34 AM
Last Post: buran
  How to write a part of powershell command as a variable? ilknurg 2 1,176 Jul-26-2022, 11:31 AM
Last Post: ilknurg
Photo Windows 10 PowerShell doesn't work Amy 3 3,997 Apr-27-2021, 01:33 PM
Last Post: jefsummers
  How to kill a bash script running as root from a python script? jc_lafleur 4 6,074 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,386 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,685 May-11-2019, 08:12 PM
Last Post: keames
  Powershell Session translation to Python; Session code seems to not work Maverick494 1 3,666 Jun-26-2018, 05:16 PM
Last Post: Maverick494
  How to run python script which has dependent python script in another folder? PrateekG 1 3,219 May-23-2018, 04:50 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020