Jump to content

Recommended Posts

Posted

I have a Powershell script that works the way I want but I am just editing it so it uses some variables so when moving between servers/sites all I need to do is edit the variables. It is pretty much working except I want one variable to call another which I can't get to work.

 

This works:

 

$StudentDefine="Pupils"

$SearchBaseOU="OU=Pupils,OU=Managed Users,DC=domain,dc=local"

$PupilsLicense = Get-MsolUser -Department Pupils -All

 

But really I want this:

 

$StudentDefine="Pupils"

$SearchBaseOU="OU=Pupils,OU=Managed Users,DC=domain,dc=local"

$PupilsLicense = Get-MsolUser -Department $StudentDefine -All

 

 

Basically I want the Get-MsolUser -Department to call the variable $StudentDefine but I can't get it to.

 

Anyone know how I can do this?

 

TIA

Posted
I have a Powershell script that works the way I want but I am just editing it so it uses some variables so when moving between servers/sites all I need to do is edit the variables. It is pretty much working except I want one variable to call another which I can't get to work.

 

This works:

 

$StudentDefine="Pupils"

$SearchBaseOU="OU=Pupils,OU=Managed Users,DC=domain,dc=local"

$PupilsLicense = Get-MsolUser -Department Pupils -All

 

But really I want this:

 

$StudentDefine="Pupils"

$SearchBaseOU="OU=Pupils,OU=Managed Users,DC=domain,dc=local"

$PupilsLicense = Get-MsolUser -Department $StudentDefine -All

 

 

Basically I want the Get-MsolUser -Department to call the variable $StudentDefine but I can't get it to.

 

Anyone know how I can do this?

 

TIA

 

I am working from memory here so might be wrong - Place () brackets around your variable, so it get's evaluated before anything else (and therefore turns the variable contents into a string in-line before the rest of the line runs).

Posted
Try putting the variable in the final line in quotation marks "$StudentDefine"

 

I thought quotes caused powershell to literally enter what you type as a string, so the string $StudentDefine would be the output instead of the variable contents?

Posted

So in the end it worked by just being by itself without being in any " or '

 

Must have been another typo somewhere as I took it out, typed it again and it worked

  • 1 month later...
Posted

Hello,

 

Can anyone help? I am trying to achieve the below:

 

1) Copy 2 newest files from Scripts to Test and show this in a log. The log must show if has failed or been a success. This log to be sent in an email.

2) Delete any files in Scripts older than 365 days

 

My Code is:

 

# Moves latest two files

 

Get-ChildItem 'C:\scripts' -File | Sort-Object -Property CreationTime -Descending | Select-Object -Last 2 | Copy-Item -Destination 'C:\TEST' -Force

 

 

$Path = "C:\scripts"

$Daysback = "-365"

$CurrentDate = Get-Date

$DatetoDelete = $CurrentDate.AddDays($Daysback)

 

Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -WhatIF

 

# Creates Log File

$logProgress = 'C:\scripts\scriptlog.log'

$source = 'C:\scripts\*.txt'

$destination = 'C:\TEST'

 

 

$txtfiles = get-childitem $source -recurse | Select-Object FullName

 

foreach ($file in $txtfiles) {Move-Item -Path $file.FullName -Destination $destination -Force -ErrorAction SilentlyContinue

 

if($? -eq $false){Write-Output "$($file.FullName) did not copy ok to $destination" | out-file -append $logProgress}

 

else

 

{write-output "$($file.FullName) copied OK to $destination" | out-file -append $logProgress}

}

  • 1 month later...
Posted (edited)
Hello,

 

Can anyone help? I am trying to achieve the below:

 

1) Copy 2 newest files from Scripts to Test and show this in a log. The log must show if has failed or been a success. This log to be sent in an email.

2) Delete any files in Scripts older than 365 days

 

My Code is:

 

# Moves latest two files

 

Get-ChildItem 'C:\scripts' -File | Sort-Object -Property CreationTime -Descending | Select-Object -Last 2 | Copy-Item -Destination 'C:\TEST' -Force

 

 

$Path = "C:\scripts"

$Daysback = "-365"

$CurrentDate = Get-Date

$DatetoDelete = $CurrentDate.AddDays($Daysback)

 

Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -WhatIF

 

# Creates Log File

$logProgress = 'C:\scripts\scriptlog.log'

$source = 'C:\scripts\*.txt'

$destination = 'C:\TEST'

 

 

$txtfiles = get-childitem $source -recurse | Select-Object FullName

 

foreach ($file in $txtfiles) {Move-Item -Path $file.FullName -Destination $destination -Force -ErrorAction SilentlyContinue

 

if($? -eq $false){Write-Output "$($file.FullName) did not copy ok to $destination" | out-file -append $logProgress}

 

else

 

{write-output "$($file.FullName) copied OK to $destination" | out-file -append $logProgress}

}

 

 

 

You could use this, it basically creates a .vbs script and then runs the vbs script to send an e-mail, no idea how you would code it strictly in powershell. But yeah, this basically uses powershell to create the vbs script and then run it. You'll have to change the e-mail addresses and password and smtp server details if you arent using gmail.

 

Echo "on error resume next" > testmail.vbs 
echo "Const schema   = `"http://schemas.microsoft.com/cdo/configuration/`"" >> testmail.vbs
echo "Const cdoBasic = 1" >> testmail.vbs
echo "Const cdoSendUsingPort = 2" >> testmail.vbs
echo "Dim oMsg, oConf" >> testmail.vbs # E-mail properties
echo "Set oMsg      = CreateObject(`"CDO.Message`")" >> testmail.vbs
echo "oMsg.From     = `"[email protected]`"" >> testmail.vbs # or "Sender Name "
echo "oMsg.To       = `"[email protected]`"" >> testmail.vbs   # or "Recipient Name "
echo "oMsg.Subject  = `"Log file`"" >> testmail.vbs
echo "oMsg.TextBody = CreateObject(`"Scripting.filesystemObject`").OpenTextFile(`"Log.txt`", 1).ReadAll" >> testmail.vbs 

# GMail SMTP server configuration and authentication info
echo "Set oConf = oMsg.Configuration" >> testmail.vbs
echo "oConf.Fields(schema & `"smtpserver`")       = `"smtp.gmail.com`"" >> testmail.vbs 

#server address
echo "oConf.Fields(schema & `"smtpserverport`")   = 465"   >> testmail.vbs           #port number
echo "oConf.Fields(schema & `"sendusing`")        = cdoSendUsingPort" >> testmail.vbs
echo "oConf.Fields(schema & `"smtpauthenticate`") = cdoBasic" >>testmail.vbs         #authentication type
echo "oConf.Fields(schema & `"smtpusessl`")       = True" >> testmail.vbs            #use SSL encryption
echo "oConf.Fields(schema & `"sendusername`")     = `"[email protected]`"" testmail.vbs #sender username
echo "oConf.Fields(schema & `"sendpassword`")     = `"Pa55w0rd`"" >> testmail.vbs     #sender password
echo "oConf.Fields.Update()" >> testmail.vbs # send message
echo "CreateObject(`"CDO.Message`").Send()" >> testmail.vbs

start testmail.vbs

Edited by Knil92

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...