0

I am trying to automate a powershell process in python. This is the process in entirety. The commands I want to automate in python are as such: 'Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\iSCSI Target' -Name AllowLoopBack -Value 1', 'New-IscsiVirtualDisk -Path "ramdisk:testRAM.vhdx" -Size 5GB', 'New-IscsiServerTarget -TargetName targetRAMDisk -InitiatorIds @("IPAddress:10.1.1.200")', 'Add-IscsiVirtualDiskTargetMapping -TargetName targetRAMDisk -DevicePath "ramdisk:testRAM.vhdx"', 'Get-IscsiTarget | Connect-IscsiTarget', 'Get-Disk | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "disk2" -Confirm:$false'

Here is what I tried:

def ramdisk_cmd(cmd,shell):
        temp_cwd = TEMP_DIR
        utl.dumplog(str(cmd))
        process = subprocess.run('Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\iSCSI Target' - 
                                  Name AllowLoopBack -Value 1',
                                 'New-IscsiVirtualDisk -Path "ramdisk:testRAM.vhdx" -Size 5GB',
                                 'New-IscsiServerTarget -TargetName targetRAMDisk -InitiatorIds 
                                  @("IPAddress:10.1.1.200")',
                                 'Add-IscsiVirtualDiskTargetMapping -TargetName targetRAMDisk - 
                                  DevicePath "ramdisk:testRAM.vhdx"',
                                 'Get-IscsiTarget | Connect-IscsiTarget',
                                 'Get-Disk | Where partitionstyle -eq 'raw' | Initialize-Disk - 
                                  PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter 
                                  -UseMaximumSize | Format-Volume -FileSystem NTFS - 
                                  NewFileSystemLabel "disk2" -Confirm:$false'
                                 )

I thought this would run the commands that I passed in as strings, but that does not seem to be the case. If I want to automate each of these commands in Python, how can I do so?

1 Answer 1

1

I would say you'll the process you start should be powershell.exe and then pass your commands as command.

process = subprocess.run('powershell.exe','-Command','Set-ItemProperty...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.