简体   繁体   中英

Trying to write a powershell script that shows all locked files with computer names instead of IP address

The task given was to create a way for our staff to see who has the file open that they want to use, as Windows says it is either locked and doesn't name the person who has it locked, or it displays the person who made the file but not the person who currently has it open.

I can look it up in Computer Management on the fileserver, but were are hoping to speed up this for the end users.

I've written this powershell script on our fileserver and it works perfectly, I have this running every 5 minutes in Task Scheduler with administrative permissions:

get-smbopenfile -ClientUserName * |select clientcomputername,clientusername,path | Out-File -Encoding utf8 "S:\LockedFiles.txt" -width 300

The output looks like this:

clientcomputername clientusername               path                                                                                                                                                                                        
------------------ --------------               ----                                                                                                                                                                                        
IPADDRESS          DOMAIN\USERNAME              S:\FOLDER\FILE.FILEEXTENSION

What I really want to do now is get the computer name rather than the IP address, just in case staff are logged into multiple machines at the same time.

I wondered if ClusterNodeName or PSComputerName would provide this, but the returned data is always blank.

I thought about this and below is one option (the first line is pseudocode), but as I see it that would mean recursively altering the piped data or reading in piped data, which I'm not even sure how to do.

$ipaddress = IPADDRESS
$Workstation = [System.Net.Dns]::GetHostByName($ipaddress)
Write-Host $Workstation.HostName

Anyone have any ideas on how I can do this? Is there a better way?

I assume you're looking to add a new property to your output object that has the resolved DNS Name from the IP Address found in the ClientComputerName property. For this you use Resolve-DnsName to attempt the name resolution and a Try Catch in case it fails to capture the exception message. For the export I would recommend you to use Export-Csv .

Get-SmbOpenFile -ClientUserName * | ForEach-Object {
    $dnsName = try {
        (Resolve-DnsName $_.ClientComputerName -ErrorAction Stop).NameHost
    }
    catch {
        [ComponentModel.Win32Exception]::new($_.Exception.NativeErrorCode).Message
    }

    [pscustomobject]@{
        ClientIpAddress  = $_.ClientComputerName
        ResolvedHostName = $dnsName
        ClientUserName   = $_.ClientUserName
        Path             = $_.Path
    }
} | Export-Csv "S:\LockedFiles.csv" -Encoding utf8 -NoTypeInformation

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