Office 365 powershell script issue

I have a powershell script that is supposed to e-mail a .csv file containing the “UserPrincipalName” & “Subscription level” of users that have been added to Office 365.

The script is running fine on a Windows 7 machine. I have been unable to get it fully working on a Windows 2012R2 server. File paths on both servers are identical.

If I run the script as-is in powershell ISE, there is no error and appears to complete, but I do not receive any e-mail.

If I modify line 19 by removing the path “office365” and run the script through the ISE, I do get an e-mail with the .csv attached, but .csv has no data (empty)

Line 19: $AccountList = “E:\batch\ADUserCreate\office365\office365reg.csv”

I also get the error below…

Rename-Item : Cannot rename the specified target, because it represents a path or device name.
At E:\Batch\ADUserCreate\Set-O365-TenantPlansV3.ps1:85 char:3
+ Rename-Item $AccountList “E:\Batch\ADUserCreate\office365\office365reg.001”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:slight_smile: [Rename-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand

 

#Get needed cmdlets
Import-Module -NAme MSOnline

Function RotateFiles{
Param ($cnt , $pth , $ext)

$p1 = $pth + "00" + $cnt
if(Test-Path $p1 -PathType Leaf){Remove-Item $p1}

for($loop=$cnt-1; $loop -ge 1; $loop--){$p1 = $pth + "00" + $loop
if(Test-Path $p1 -PathType Leaf){$p2 = $pth +"00" + $cnt
Rename-Item $p1 $p2};$cnt=$loop}

$p1 = $pth + $ext; $p2 = $pth + "001"
if(Test-Path $p1 -PathType Leaf){Rename-Item $p1 $p2}
}

$AccountLog = "E:\Batch\ADUserCreate\logs\Office365_reg_log.txt"
$AccountList = "E:\batch\ADUserCreate\office365\office365reg.csv"

$StandPW = Get-Content "E:\batch\login\pw.txt" | ConvertTo-SecureString
$USerCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist “account”,$StandPW

#Check if account file exists and perform deletes and renames of old files if it does
if(Test-Path $AccountList -PathType Leaf){$Users = get-content $AccountList

RotateFiles -cnt "26" -pth "E:\batch\ADUserCreate\office365\office365reg." -ext "donotrename"

#Check if old account log file exists and perform deletes and renames of old files if it does
RotateFiles -cnt "26" -pth "E:\Batch\ADUserCreate\office365\office_365reg_log." -ext "txt"

#$UserCredential = Get-Credential
Connect-MSolService -Credential $UserCredential
$AccountSkuId = "DOMAIN:DESKLESSPACK"
$UsageLocation = "US"
$ATPAccountSkuID = "DOMAIN:ATP_ENTERPRISE"

#$Users = Import-Csv E:\Batch\ADUserCreate\office365\office365regtst.csv
$Users = Import-Csv $AccountList

$Users | ForEach-Object {
#$LO = New-MsolLicenseOptions -AccountSkuId DOMAIN:STANDARDPACK -DisabledPlans FORMS_PLAN_E1,STREAM_O365_E1,Deskless,FLOW_O365_P1,POWERAPPS_O365_P1,TEAMS1,PROJECTWORKMANAGEMENT,SWAY,YAMMER_ENTERPRISE
#$LO = New-MsolLicenseOptions -AccountSkuId DOMAIN:STANDARDPACK -DisabledPlans FORMS_PLAN_E1,STREAM_O365_E1,Deskless,FLOW_O365_P1,POWERAPPS_O365_P1,TEAMS1,PROJECTWORKMANAGEMENT,SWAY,YAMMER_ENTERPRISE
$LO = New-MsolLicenseOptions -AccountSkuId DOMAIN:DESKLESSPACK -DisabledPlans STREAM_O365_K,DeskLess,TEAMS1,SWAY,YAMMER_ENTERPRISE
Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $UsageLocation 
Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId, $ATPAccountSkuID -LicenseOptions $LO
#Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId, $ATPAccountSkuID
#Add some error checking
$LO=""

}
#SMTP server name
$smtpServer = "mail server"

#Creating a Mail object
$msg = new-object Net.Mail.MailMessage

#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

#Setup email text
$EmailText = "See attached files"

$msg.To.Add("jane.doe@domain.com")

#Email structure
$msg.From = "server@DOMAIN.com"

$msg.subject = "Office365"
$msg.body = $EmailText

#Add attachment
$MsgAtt1 = new-object Net.Mail.Attachment($AccountList)
$msg.Attachments.Add($MsgAtt1)
#$MsgAtt2 = new-object Net.Mail.Attachment($AccountLog)
#$msg.Attachments.Add($MsgAtt2)

#Sending email
$smtp.Send($msg)

#Remove Message from Memory
$MsgAtt1.Dispose()
#$MsgAtt2.Dispose()

Rename-Item $AccountList "E:\Batch\ADUserCreate\office365\office365reg.001"
}

Are you sure that path actually exists the way you have it specified.
Removing that office365 leaf, says that it found the file, vs the

This is a bit overkill…

#Check if account file exists and perform deletes and renames of old files if it does
if(Test-Path $AccountList -PathType Leaf)
{$Users = get-content $AccountList

… since you are directly UNC to the path. SO, you should only need this …

#Check if account file exists and perform deletes and renames of old files if it does
if(Test-Path $AccountList)
{$Users = get-content $AccountList

… since you are trying to read the file.
I get the to mission of that -leaf is ‘To explicitly make sure it’s a file and not a directory’, but you already know that, since by definition in your specification, your $AccountList variable which is a full UNC.

By using -Leaf, you are saying that you believe someone/ thing may have deleted or moved that file. What permissions are on the UNC path to prevent anything but you or your code from touching it?