How to run PS1 command within a PS1 script

Hello.  I have setup multiple ps1 scripts which connect to O365/MSOL PowerShell and allows me to manipulate individual schools accounts.

I wish to be able to collect these PS1 files together using the LS *ps1 command and use the Name to fire a powershell command and look for the accounts which begin with office@

LS *ps1 work in a variable and I can look at the names it provides by doing $cmd = ls *ps1

$cmd.Name

I cannot get this to fire a powershell command or run each ps1 as a foreach loop.

can anyone help please

I cd into the directory which contains the PS1 scripts.

Thank you in advance

September 3rd, 2015 8:56am

Please show the script that you have written so far.

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 9:04am

Hi,

Here's something you can play with:

Get-ChildItem *.ps1 | ForEach {

    & $_.FullName

}


Test files:
Mode         LastWriteTime Length Name 
----         ------------- ------ ---- 
-a---   9/3/2015   9:02 AM     30 1.ps1
-a---   9/3/2015   9:02 AM     30 2.ps1
-a---   9/3/2015   9:02 AM     30 3.ps1

1.ps1:
Write-Host 1

2.ps1
Write-Host 2

3.ps1
Write-Host 3

Example output:
1
2
3


September 3rd, 2015 9:06am

cheers for getting back to me so quickly.

The script I have so far is

----------------------------------------------------

cd \fso\PSConnections

$lscmd = ls *ps1

ForEach ($i in $lscmd) {
powershell.exe "$i"
Get-MsolUser |where {$PSItem.UserPrincipalName -like "*office*"} |select UserPrincipalName
get-pssession |remove-pssession
}

------------------------------------------------------------

So

1.  I cd into the files location.  run the ls variable to capture all ps1 files.  This works

2. a foreach statement runs $I in $lscmd Then run a powershell.exe "$I"

3. Then do a search when you have connected to the O365 environment and look for an office user.

4. try to remove the session.  upto option 3 works once but pulls in the same Office account every time.  EG office@mydomain365-1.com even though each loop logs into a different domain EG.  domain1 = office@mydomain365-1.com then domain2 = office@mydomain365-1.com where it should say office@mydomain365-2.com

its like it is not removing the sessions.  I might try exit after the Get-MsolUser command to see if that releases the powershell.exe session.

Thanks again Mike most appreciated

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 9:29am

Sorry Larry.

The script that runs within the primary PS1 script co0nnects to the O365 environment:-

this script connects to the school which then allows the Primary script to run the Get-MsolUser command

----------------------------------------------------------------------------

#connect to the exchange online service for NameOfSchool
$exchange = New-PSSession -Name NameOfSchool -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell" -Credential (Import-Clixml C:\fso\PSConnections\NameOfSchool.xml) -Authentication basic -AllowRedirection

Import-PSSession $exchange -AllowClobber

Get-Module
Write-host ""
Write-host "You have successfully connected to the O365 Exchange service " -F Green
Write-Host "There are:- " -F Green -nonewline;Write-Host  (get-mailbox).count -F Yellow -nonewline;Write-Host " Mailboxes for NameOfSchool" -F Green
# Connect to MSOnline
Write-Host ""
Import-Module MSOnline
$365Logon = get-credential -Credential (Import-Clixml C:\fso\PSConnections\NameOfSchool.xml)
Connect-MsolService -Credential $365Logon
Write-Host ""
Get-MsolCompanyInformation |fl DisplayName
Get-MsolAccountSku |where {$_.AccountSkuId -like "*standard*"} |fl AccountSkuId

----------------------------------------------------------------------------

Hope this helps.

September 3rd, 2015 10:12am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics