简体   繁体   中英

Get-ChildItem working but Import-csv defaulting to home dir Powershell

I'm trying to email a report that gets generated every day. Also, trying to count the rows of users in the csv as well as create logic to only count and send the latest report.

Although the variables appear to have the correct values in them regarding the file name, when I check after running this script, I keep getting an error on the Import-csv saying it can't find the file in the default user directory and not the path I'm specifying. It's rather annoying. Please help. Thanks

Add-PSSnapin Quest.ActiveRoles.ADManagement
$reports = "\\Server\G$\Marc\Scripts\DailyReports\active\"
$latest = Get-ChildItem -Path $reports | Sort-Object -Descending | Select-Object -First 1


$people = Get-QADUser -SizeLimit 0 -LdapFilter "(&(employeeID=*)(&(objectCategory=person)(objectClass=user)(!objectClass=inetOrgPerson))(sAMAccountName=*))” -includedproperties dn,sn,title,givenname,company,employeeID,primarysmtpaddress,SamAccountName
$people | select dn,sn,title,givenname,company,employeeID,primarysmtpaddress,@{Label="SamAccountName";Expression={$_.SamAccountName.ToLower()}} | ConvertTo-Csv -NoTypeInformation | select -Skip 1 > \\Server\G$\Marc\Scripts\DailyReports\active\Total-Active-Users-AD_$((Get-Date).ToString('MM-dd-yyyy')).csv


$TotalUsers = Import-csv -Path $Latest.name | Measure-Object | Select-Object -expand Count | Out-String

send-mailmessage -from "email@me.com" -to "email@me.com" -subject "Total AD Active Users Report " -body "Total Active Users today was $TotalUsers" -Attachments $Latest.name -priority High -dno onSuccess, onFailure -smtpServer Smtp.server.com

Is your script running from the same directory where your reports are stored? If not, this could be your problem:

$Latest.Name will give you the name of the file only, ie "report.txt" or whatever your report is named. If you run Import-Csv and give it a file name without an absolute path, then Powershell will expect to find that file in the current directory (the default user directory in your case).

$Latest.FullName will give you the absolute path to the file. Try using it as the path parameter for Import-Csv .

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