3

So, I've found my perfect PowerShell script to archive Groups in a really nice way. The PS script is interactive and "asks some questions" (Prompt command), so to speak. :) The script looks as follows:

$CheckGroup = Read-Host -Prompt "Enter alias of group to archive"

$AGroup = (Get-UnifiedGroup $CheckGroup -ErrorAction SilentlyContinue)

If ($AGroup) {

Write-Host "Archiving" $AGroup.DisplayName -ForegroundColor Yellow

} Else {

Write-Host $CheckGroup "group not found - terminating"

Return }

# Get lists of current owners and members

$CurrentOwners = (Get-UnifiedGroupLinks -Identity $AGroup.Alias
-LinkType Owners | Select Name)

$CurrentMembers = (Get-UnifiedGroupLinks -Identity $AGroup.Alias
-LinkType Members | Select Name)

# Add a new owner - this is the address of the account that will continue to access the group

$AdminAccount = "Compliance Administrator"

Add-UnifiedGroupLinks -Identity $AGroup.Alias -LinkType Members -Links $AdminAccount

Add-UnifiedGroupLinks -Identity $AGroup.Alias -LinkType Owners -Links $AdminAccount

# Remove the other members and owners

ForEach ($O in $CurrentOwners) {

Remove-UnifiedGroupLinks -Identity $AGroup.Alias -LinkType Owners
-Links $O.Name

-Confirm:$False}

ForEach ($M in $CurrentMembers) {

Remove-UnifiedGroupLinks -Identity $AGroup.Alias -LinkType Members
-Links $M.Name

-Confirm:$False}

# Create SMTP Address for the archived group

$OldSmtpAddress = $AGroup.PrimarySmtpAddress -Split "@"

$NewSmtpAddress = $OldSmtpAddress[0] + "_archived" + "@" + $OldSmtpAddress[1]

$AddressRemove = "smtp:"+$AGroup.PrimarySmtpAddress

# Update Group properties

Set-UnifiedGroup -Identity $AGroup.Alias -AccessType Private
-RequireSenderAuthenticationEnabled $True -HiddenFromAddressListsEnabled $True -CustomAttribute1 "Archived" -CustomAttribute2 (Get-Date -Format s) -PrimarySmtpAddress $NewSmtpAddress

Set-UnifiedGroup -Identity $AGroup.Alias -EmailAddresses @{remove=$AddressRemove}

Write-Host $AGroup.DisplayName "is now archived and" $AdminAccount "is the new group owner

My question:

I wonder, If there's a possibility to make this script "interactive" on an ordinary Sharepoint site? So the assistant can go to this site, execute the PS script and run it completely on the site?

1 Answer 1

2

There is a way. Although it's theoretical.

  1. Modify your PowerShell script so that it also hosts a mini-web server.

You can use two PowerShell modules for that:

Import-Module Polaris -Verbose
Import-Module PSHTML -Verbose
Add-Type -AssemblyName System.Web

http://chen.about-powershell.com/2018/09/exploring-pshtml-for-polaris-yet-another-way-to-build-forms-using-powershell/ https://chen.about-powershell.com/2018/09/building-a-simple-form-using-powershell-polaris-module/

Example. Credits to Chendrayan Venkatesan:

Import-Module Polaris -Verbose
Import-Module PSHTML -Verbose
Add-Type -AssemblyName System.Web

New-PolarisGetRoute -Path "/Login" -Scriptblock {
    $HTML = html {
        head {
            Title "Login Page"
        }

        body {
            h1 -Content "Connect to Office365 Securely..."
            Form -action "/authenticate" -method 'post' -target '_blank' -Content {
                input -type text -name 'uSite'
                input -type text -name 'uName' 
                input -type password -name 'uPassword'
                button -Content "Click"
            }
        }
    }
    $Response.SetContentType('text/html')
    $Response.Send($HTML)
}

New-PolarisPostRoute -Path "/authenticate" -Scriptblock {
    $Response.SetContentType('application/json')
    $Body = [System.Web.HttpUtility]::UrlDecode($Request.BodyString)
    $Data = @{}
    $Body.split('&') | ForEach-Object {
        $part = $_.split('=')
        $Data.add($part[0] , $part[1])
    }
    Import-Module C:\projects\iPolaris\assemblies\Microsoft.SharePoint.Client.dll
    $UserName = $Data.uName 
    $Pasword = $Data.uPassword | ConvertTo-SecureString -AsPlainText -Force 
    $ClientContext = [Microsoft.SharePoint.Client.ClientContext]::new($Data.uSite)
    $ClientContext.Credentials = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($UserName, $Pasword)
    $ListCollection = $ClientContext.Web.Lists
    $ClientContext.Load($ListCollection)
    $ClientContext.ExecuteQuery()
    $ClientContext.Dispose()
    $Collection = @()
    foreach ($List in $ListCollection | Select-Object -First 10) {
        $Result = [pscustomobject]@{
            Title     = $List.Title 
            ItemCount = $List.ItemCount
        } 
        $Collection += $Result
    }
    $Response.Send(($Collection | ConvertTo-Json))
}

Start-Polaris -Port 8080
  1. Make sure that your mini-webserver checks the URLs. For example, you can pass parameters to your PowerShell script with URL: http://hostName:8080/GroupToArchive

  2. In SharePoint, you can add some simple HTML Link

    <a href='http://hostName:8080/GroupToArchive'>Click to archive this SharePoint site</a>

As you can see, this is just a general idea, but you can steal it to implement it the way it works for you.

1
  • I was trying to test the PSHTML with Polaris. When I execute the below code the first time it works good. And when I try to run the second time it throws error as: > Exception calling "Start" with "0" argument(s): "Failed to listen on > prefix 'localhost:8080' because it conflicts with an existing > registration on the machine." I even tried running Stop-Polaris cmdlet but it didn't work. Any suggestions for me, please? Import-Module Polaris -Verbose New-PolarisGetRoute -Path "/helloworld" -Scriptblock { $Response.Send('Hello World!') } Start-Polaris -Port 8080
    – Hari
    Sep 27, 2019 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.