简体   繁体   中英

Changing a Powershell Script into a Python Script

I am trying to convert a powershell script into a python script, can someone explain how this command line works?

Also can someone explain me the python command to do the same thing?

get-childitem \\$server\siglocal$\MACAutoTicket -recurse | where {$_.name -like "MACAutoTicket.$date.$server.macautoticket.*"} |

Explaining the Powershell:

get-childitem \\$server\siglocal$\MACAutoTicket -recurse 

This will list all the files including subdirectories on a remote computer that's passed as variable $server .

The results are then passed to a filter

where {$_.name -like "MACAutoTicket.$date.$server.macautoticket.*"}

Which in turn will include only such files that match a specific name pattern:

"MACAutoTicket.$date.$server.macautoticket.*"

The file name should contain string literals and likely a date obtained from $date and the server's name obtained from the forementioned $server .

In order to proceed with Python conversion, find out where $server and $date are obtained. Maybe they are script parameters or read from some configuration file.

Python's fnmatch looks like a good starting point for creating similar a filter for file names.

vonpryz has a good answer, I'd add to it a little bit.

name is a property that the child items all have; this script only looks at name. If you want a list of all the properties, running the command get-childitem \\\\$server\\siglocal$\\MACAutoTicket | get-member get-childitem \\\\$server\\siglocal$\\MACAutoTicket | get-member will list all the properties available to you.

You probably already know this, but -recurse will look at all the child directories and get their content as well. Keep in mind that the get-childitem command is more than just a "dir" command - it can also get child items from a parent in the registry, such as HKLM.

The question is somewhat broad so I'll also add an explanation of the pipe | character. This takes the output from one command and makes it the input to the next command. You can string together a long list of commands doing this where the next cmdlet takes the output from the previous, does its processing, spits out the result, and then is processed by the next cmdlet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM