Setting default for StrongAuthenticationMethods via Powershell - azure

I'm using the following script to enable MFA methods for a user in Azure Active Directory (snippet taken from this article):
Connect-MsolService
$UserPrincipalName = "j.brown#exchangelabs.nl"
$SMS = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationMethod
$SMS.IsDefault = $true
$SMS.MethodType = "OneWaySMS"
$Phone = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationMethod
$Phone.IsDefault = $false
$Phone.MethodType = "TwoWayVoiceMobile"
$PrePopulate = #($SMS, $Phone)
Set-MsolUser -UserPrincipalName $UserPrincipalName -StrongAuthenticationMethods $PrePopulate
It works great, but this means that I'll have to run this anytime a new user is created.
Is there a way I can set it as a default for new users?
Edit:
Here is a previous question that might shed some more light on the issue:
Enforcing phone number in azure active directory MFA

Have you tried the below snippet at the end,
Connect-MsolService
$UserPrincipalName = "email#email.com"
$SMS = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationMethod
$SMS.IsDefault = $true
$SMS.MethodType = "OneWaySMS"
$Phone = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationMethod
$Phone.IsDefault = $false
$Phone.MethodType = "TwoWayVoiceMobile"
$PrePopulate = #($SMS, $Phone)
Get-MsolUser -All | foreach{set-msoluser -UserPrincipalName $_.UserPrincipalName -StrongAuthenticationMethods $PrePopulate}

Related

How can i export Event Logs using powershell

i'm new to ps scripting , i want to export certain data from event logs. I tried following
$Myexcel = New-Object -ComObject excel.application
$Myexcel.visible = $true
$Myworkbook = $Myexcel.workbooks.add()
$Sheet1 = $Myworkbook.worksheets.item(1)
$Sheet1.name = "summary"
$Sheet1.cells.item(1,1) = 'BootDevice'
$events = Get-WinEvent -FilterHashtable #{logname="Microsoft-Windows-Storage-Storport/Health"; id=511}
# get the first event raw XML
$event = [xml]$events[0].ToXml()
# display its content
#$event.Event.EventData.Data
$BootDevice=$event.Event.EventData.Data | Where-Object {$_.name -eq "BootDevice"}
write-output $BootDevice
$Sheet1.cells.item(2,1) = $BootDevice
$Sheet1.Columns.AutoFit()
$Myfile = 'E:\tmp\test.xlsx'
$Myworkbook.Saveas($Myfile)
$Myexcel.displayalerts = $true
But its giving error
Exception from HRESULT: 0x800A03EC
At line:16 char:1
+ $Sheet1.cells.item(2,1) = $BootDevice
And a blank excel is generated.
Any help will be thankfull.

How to set a look up column in sharepoint by passing the lookup value using CSOM?

How to set a look up column in share point by passing the lookup value using CSOM Powershell?
I Have a list where i want to insert an item with a lookup value for the lookup column, how to achieve this using CSOM Power shell?
You can insert an item and set its lookup column via below code:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Mysite URL
$site = 'https://abc.sharepoint.com/sites/s01'
#Admin User Principal Name
$admin = 'admin#abc.onmicrosoft.com'
#Get Password as secure String
$password = ConvertTo-SecureString "xxxxxxxxx" -AsPlainText -Force
#Get the Client Context and Bind the Site Collection
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
$context.Credentials = $credentials
$list = $context.Web.Lists.GetByTitle('SeriesArticle')
$context.Load($list)
$context.ExecuteQuery()
$listItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$NewItem = $list.AddItem($listItemInfo)
$NewItem["Title"]="t001"
# create an Object[] and add FieldLookupValue instances
$lookupValueCollection = #()
$lookupValue = New-Object Microsoft.SharePoint.Client.FieldLookupValue
$lookupValue.LookupId = 4
$lookupValueCollection += $lookupValue
$lookupValue = New-Object Microsoft.SharePoint.Client.FieldLookupValue
$lookupValue.LookupId = 5
$lookupValueCollection += $lookupValue
# convert the Object[] to a FieldLookupValue[]
$mvLookup = [Microsoft.SharePoint.Client.FieldLookupValue[]]$lookupValueCollection
$item["MultiValueLookupColumnName"] = $mvLookup
$item.Update()
$ctx.ExecuteQuery()
More Reference:
https://www.sharepointdiary.com/2017/03/sharepoint-online-powershell-to-update-lookup-field.html
BR

Need a little help adding a table range from an excel file on sharepoint to an email body with powershell

$if = '\\portal2010.brand.com\sites\HC\2017 count.xlsx'
$excel = New-Object -Com Excel.Application
$Workbook = $Excel.Workbooks.Open($if)
$page = 'HC'
$ws = $Workbook.worksheets | where-object {$_.Name -eq $page}
$range = $ws.Range("V2:AF")
$rows = $range.Rows.Count
$hcTableCopy = $ws.Range("V2:AF$rows").Copy()
$hcTablePaste = $hcTableCopy.PasteSpecial($default, $default, $default, $default, 9, $default, $default)
$SendTo = "e.mail#brand.com"
$SMTPServer = "smtp.brand.com"
$EmailFrom = "e.mail2#brand.com"
$EmailSubject = "Weekly Email"
$Image2 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 HC_files\2017 count_25311_image002.png'
$Image4 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 HC_files\2017 count_25311_image004.png'
$Image6 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 HC_files\2017 count_25311_image006.png'
$Image8 = '\\portal2010.brand.com\sites\HC\HC Dashboards\2017 HC_files\2017 count_25311_image008.png'
$Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$att2 = new-object Net.Mail.Attachment($Image2)
$att2.ContentId = "att2"
$att4 = new-object Net.Mail.Attachment($Image4)
$att4.ContentId = "att4"
$att6 = new-object Net.Mail.Attachment($Image6)
$att6.ContentId = "att6"
$att8 = new-object Net.Mail.Attachment($Image8)
$att8.ContentId = "att8"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$body = '
<img src="cid:att2" /><br/><br/>
<img src="cid:att4" /><br/><br/>
<img src="cid:att6" /><br/><br/>
<img src="cid:att8" /><br/><br/>
'
$Message.From = $EmailFrom
$Message.To.Add($SendTo)
$Message.Cc.Add($CCTo1)
$Message.Cc.Add($CCTo2)
$Message.Cc.Add($CCTo3)
$Message.Cc.Add($CCTo4)
$Message.Subject = $EmailSubject
$Message.Body = $body + $hcTablePaste
$Message.IsBodyHTML = $true
$Message.Attachments.Add($att2)
$Message.Attachments.Add($att4)
$Message.Attachments.Add($att6)
$Message.Attachments.Add($att8)
$smtp.Send($Message)
$excel.DisplayAlerts = $False
Start-Sleep -s 5
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
The above code is what I have tried so far. I get an error that says it can't open the file from sharepoint at this point and I am not sure the table paste portion of this is going to work right.
I tried modifying the following, but I really don't think I should have to check out and make editable the file just to copy the table range to an email.
foreach ($file in $excelfiles)
{
$workbookpath = $file.fullname
if ($excel.workbooks.canCheckOut($workbookpath)) {
# open the worksheet and check it out
$excelworkbook = $excel.workbooks.Open($workbookpath)
$excelworkbook = $excel.workbooks.CheckOut($workbookpath)
# Don't ask cuz I don't know (yet). You have to open it again.
$excelworkbook = $excel.workbooks.Open($workbookpath)
# Refresh all the pivot tables with the new data.
$excelworkbook.RefreshAll()
# Save and Check it in
$excelworkbook.Save()
$excelworkbook.CheckInWithVersion()
}
}
$excel.quit()
So any help to get the table pasting part would be great. The email send with images just fine other than that.

Powershell WinForms UI refresh / live update

I'm currently pulling my hair out trying to update a Winforms UI using the 'Register-objectevent' Cmdlet.
What I'm trying to do is get the Register-ObjectEvent to update the label in the form ever tick on the timer.
I've done hours of research on this, and I know it's something to do with multithreading / invoking, but I can't get my head around how to make it work !
If someone could show me / help me to get this script to update the label on the form by the timer, that would be amazing ! I've got lots of Winforms that would benifit from multithreading, but I need to get my head around it first !
Here's the script I'm trying to get working, any help is greatly appreciated :)
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles() | out-null
$form1 = New-Object System.Windows.Forms.Form
$OnLoadForm_StateCorrection=
{
$form1.WindowState = $InitialFormWindowState
}
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 600
$System_Drawing_Size.Width = 1200
$form1.ClientSize = $System_Drawing_Size
$form1.MaximizeBox = $False
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.KeyPreview = $True
$form1.FormBorderStyle = 1
$form1.Name = "form1"
$form1.StartPosition = 1
$form1.backcolor = [System.Drawing.Color]::FromArgb(255,240,240,240)
$timer = New-Object System.Timers.Timer
$timer.Interval = 1000
$timer.AutoReset = $true
$timeout = 0
$num=0
$action = {
$num++
write-host "test"
$vollabel.text=$num
$timer.stop()
}
Register-ObjectEvent -InputObject $timer -SourceIdentifier TimerElapsed -EventName Elapsed -Action $action
$timer.start()
$vollabel = New-Object System.Windows.Forms.Label
$vollabel.Location = "0,0"
$form1.Controls.Add($vollabel)
$InitialFormWindowState = $form1.WindowState
$form1.add_Load($OnLoadForm_StateCorrection)
$form1.Add_Shown({$form1.Activate()})
$form1.ShowDialog()| Out-Null
I pared down your script a bit for a working proof of concept - add back in anything you needed:
#('System.Drawing','System.Windows.Forms') | %{ [reflection.assembly]::LoadWithPartialName($_) | Out-Null }
[System.Windows.Forms.Application]::EnableVisualStyles() | out-null
$form1 = New-Object System.Windows.Forms.Form -Property #{
MaximizeBox = $False
KeyPreview = $True
FormBorderStyle = 1
Name = "form1"
StartPosition = 1
backcolor = [System.Drawing.Color]::FromArgb(255,240,240,240)
ClientSize = New-Object System.Drawing.Size -Property #{Height = 600;Width = 1200}
}
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$timer = New-Object System.Windows.Forms.Timer -Property #{Interval = 1000} #Forms.Timer doesn't support AutoReset property
$script:num=0 #scope must be at script level to increment in event handler
$timer.start()
$timer.add_Tick({
$script:num +=1
write-host "test $script:num"
$vollabel.text=$script:num
})
$vollabel = New-Object System.Windows.Forms.Label -Property #{Location = "0,0"}
$form1.Controls.Add($vollabel)
$form1.ShowDialog()| Out-Null
$timer.stop() #This will keep running in the background unless you stop it
A few notes:
Form.ShowDialog() is blocking and stops the script execution.
System.Windows.Forms.Timer has slightly different properties than System.Timers.Timer and can take a ScriptBlock or a function name as a parameter to add_Tick()
An event handler ScriptBlock has its own scope, but you can share variables with the $ScopeName:VariableName syntax. I couldn't get $num to increment unless I set the scope to $Script

Fetching data from Excel stored in Document Library and displaying it in a list using powershell

I have to fetch data from an Excel document which is stored in my Document Library and display the data in a list. My script runs fine but at $workbook it prompts me to pass credentials.
Can this issue be solved?
$web = Get-SPWeb -Identity 'http://.....'
$list = $web.Lists['XL List']
$XLSDoc ='http://...'
$SheetName = 'Overall Project Health'
$Excel = New-Object -ComObject 'Excel.Application'
$Workbook = $Excel.workbooks.open($XLSDoc)....
$Sheet = $Workbook.Worksheets.Item($SheetName)
Try this:
$Workbook = $Excel.workbooks.open($XLSDoc, $false, $true)
Reference:
http://msdn.microsoft.com/en-us/library/aa195811(office.11).aspx

Resources