Query multiple SQL Server instances using Powershell referencing the list of instances in a text file

Rather than issuing a request per instance via SQL Server Management Studio, we are trying to create a PowerShell script to query multiple instances by going through a loop referencing a text file where it contains, say, 100 SQL Server instances. Browse each instance of SQL Server, issue a query, and export to CSV.

Here is the PowerShell script we currently have:

$ ServerInstance = "C: Users \ Documents InstanceList.txt"

foreach ($ i in $ ServerInstance)
{

$ sql = "SELECT
DB_NAME (dbid) as DBName,
COUNT (dbid) as NumberOfConnections,
RTRIM (loginame) as LoginName,
RTRIM (Hostname) As HostName, Login_Time, Program_name
OF
sys.sysprocesses
OERE –DB_NAME (dbid) = & # 39; genesys_wfm & # 39; and
dbid> 5
– and HostName = & # 39; xxxx & # 39;
and the login name is not in (& # 39; NT AUTHORITY SYSTEM & # 39 ;, & # 39; ACE & # 39 ;, & # 39; domain xxxx & # 39;)
BY GROUP
dbid, loginame, Hostname, Login_Time, Program_name
order by Login_Time desc; "

Invoke-Sqlcmd -ServerInstance $ i -Query $ sql -ConnectionTimeout 60 -QueryTimeout 99999

The following is the InstanceList.txt:
server_name instance name1
servername2 instance name2
And so on.

Thank you, I hope my explanation helps.

Dennis

powershell – problem with robocopy on copying files from source to long distance destination

I am using robocopy $ source $ destination / MIR, the fact is that it copies the files in synchronization with the source but ignores the files and directories of the source which have long path names.

i wish i had a fix for that, i tried several ways but it didn't work.

Is there anything wrong with this PowerShell script?

I have the following code in the project that installs sl on Windows using PowerShell. Is there anything you could improve on this?

param((String)$Profile, (Switch)$Help, (Switch)$Force)

$ErrorMessages = @()

$Payload = "
# 
function Steam-Locomotive {wsl sl -e}
function Steam-Locomotive-Force {wsl sl}
# 
"

$HelpText = "This script helps you use the tremendous `"sl`" program in Windows PowerShell.
Simply download the .ps1 file and execute it.
If the script finishes successfully, you can type Steam-Locomotive in PS to start the interruptable `"sl -e`".
Use Steam-Locomotive-Force to prevent interruption (`"sl`").
Example:
PS C:UsersUserDownloads> .Install-Steam-Locomotive.ps1 -Profile $PROFILE
"

if ($Help) {
  Write-Output $HelpText
  exit
}

if (!(Get-Command -Name "wsl" -CommandType "Application" -ErrorAction SilentlyContinue)) {
  $ErrorMessages += @("You don't have WSL installed. Cannot continue.")
}

if (!$Profile) {
  $ErrorMessages += @("Please supply your profile location under -Profile. Cannot continue.")
  if (Get-Content $Profile -ErrorAction SilentlyContinue | Select-String "Steam-Locomotive") {
    $ErrorMessages += @("Your profile seems to already contain something called `"Steam-Locomotive`". Use -Force to continue anyways.")
  }
}

if ((Get-Command -Name "Steam-Locomotive" -ErrorAction SilentlyContinue) -and !$Force) {
  $ErrorMessages += @("It seems a command named `"Steam-Locomotive`" is already installed. Use -Force to continue anyways.")
}

if ($ErrorMessages) {
  Write-Output $ErrorMessages
  exit
}

if (!(wsl command -v sl)) {
  Write-Output "First, install SL."
  wsl sudo apt install sl
}

if (wsl command -v sl) {
  Add-Content $Profile $Payload
  Write-Output "Done!"
}

if (!(wsl command -v sl)) {
  Write-Output "Failed installing wsl."
  exit
}

Write-Output "Success!"

How to add a list column to list the content type via SharePoint Online PowerShell?

I am trying to add a custom property to list the content type on the SharePoint Online site by following the script:

$context=New-Object Microsoft.SharePoint.Client.ClientContext($URL)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
$context.ExecuteQuery()

$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()

$lib = $web.Lists.GetByTitle("Document Library")

$context.Load($lib)
$context.ExecuteQuery()

$cts = $lib.ContentTypes
$context.Load($cts)
$context.ExecuteQuery()

ForEach ($c in $cts) {
     if($c.Name -eq "Document") {
        $ct = $c
    }
}

$fields = $lib.Fields
$context.Load($fields)
$context.ExecuteQuery()

ForEach ($fl in $fields) {
    if($fl.Title -eq "Keyword") {
        $keyword = $fl
        $context.Load($keyword)
        $context.ExecuteQuery()
    }
}

$link=new-object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$link.Field=$keyword

$ct.FieldLinks.Add($link)
$ct.Update($false)
$context.ExecuteQuery()

But I had the exception below when running.

Exception setting "Field": "Cannot convert the
"Microsoft.SharePoint.Client.FieldText" value of type
"Microsoft.SharePoint.Client.FieldTe xt" to type
"Microsoft.SharePoint.Client.Field"." At D:tmpAddContentType.ps1:47
char:44
+ (cultureinfo)::CurrentUICulture = 'en-US'; $link.Field=$keyword
+                                            ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) (), SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting   Cannot convert argument "parameters", with value:
"Microsoft.SharePoint.Client.FieldLinkCreationInformation", for "Add"
to type "Microsof t.SharePoint.Client.FieldLinkCreationInformation":
"Cannot convert the
"Microsoft.SharePoint.Client.FieldLinkCreationInformation" value o f
type "Microsoft.SharePoint.Client.FieldLinkCreationInformation" to
type "Microsoft.SharePoint.Client.FieldLinkCreationInformation"." At
D:tmpAddContentType.ps1:51 char:1
+ $ct.FieldLinks.Add($link)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) (), MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

The examples I have found so far all relate to adding site columns to the content type, not the list column.
Is there a way to add a list column to the list content type via PowerShell?

Thanks in advance.

How to automate the SharePoint document library with a local folder using PowerShell or some other?

I want to create a solution where the documents from the document library will automatically synchronize with the local folder. Synchronization is not only to be downloaded at the same time, whatever changes are made to the synchronized document, they must be uploaded to SharePoint. I found few articles but these talk about how to download them only.

Is there anyway that I can achieve this via PowerShell or maybe with the onedrive API if available?

https://powershell.org/forums/topic/backup-sp-online-document-library-locally/
https://www.c-sharpcorner.com/blogs/get-all-documents-from-a-sharepoint-document-library-to-a-local-folder-using-powershell

powershell – Set-OrganizationConfing not recognized, even if I can get-OrganizationConfig

I am trying to set the "Targeted Inbox" to disabled for the entire organization. I have found many scripts and tutorials online, most of which point to using the Exchange Online session in PowerShell.

for example.:

My PS script is as follows:

# Set up Credential
$UserCredential = Get-Credential

# Create the Exchange Online session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

# Import the cmdlets into this session
Import-PSSession $Session -DisableNameChecking

# Get a list of the organisation's current settings
Get-OrganizationConfig

# Now set the 'Focused Inbox' option to 'off' for everyone
Set-OrganizationConfig -FocusedInboxOn $false

# Must remove the session to avoid clogging and clutter
Remove-PSSession $Session

The "Get-OrganizationConfig" command returns a complete list of the current configuration:

enter description of image here

However, when I run the "Set-OrganizationConfig" command, it fails with the following message:

Set-OrganizationConfig : The term 'Set-OrganizationConfig' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 

path has been included, verify that the path is correct and try again.

enter description of image here

My presumption is that this is a licensing issue, however, I can see all the configuration of the organization, and it is the cmdlet that is missing.

What's the problem?

I am:

  • Running PS as an administrator
  • Use of "SharePointAdmin" administrator account for credentials

Temporarily disable the connection to the domain via Powershell

I have many computers (Windows 10 Edu) in my Active Directory and I need to disable their ability to connect to their domain temporarily via PowerShell.
Only local accounts should be able to connect.
I also need a powershell command to re-authorize the connection to the domain.

Removing them from the domain is not an option.

Powershell commands are executed on a local administrator account and should not involve any user interface or confirmation with the mouse or keyboard.

Windows – Cannot run Power-Shell Get-NLBClusterNode remotely

I have created a small cluster of 2 Win servers in one domain and another for remote executions.

I can run Get-NlbClusterNode locally on each of the servers.
I can establish a remote session with the 2 nodes and have all the permissions in place.
I can run the Get-NlbClusterNode command remotely if only one of the servers is in the cluster, but when both are in the cluster, I get:

PS C: Windows system32> Invoke-Command -ComputerName 10.10.10.9 -ScriptBlock {Get-NlbClusterNode} -credential $ Creds Access is denied. . : 10.10.10.9

Is this what is called the PowerShell double hop problem? How could we solve this problem?

Thank you

office365 – Unable to remove delegate calendar access via PowerShell in o365

I have read the documentation from Microsoft and this is supposed to be the command:

set-mailboxfolderpermission userA@contoso.com:calendar -user userB@contoso.com -accessrights editor -sharingpermissionflags none

userA is administrator, PowerShell is connected to the exchange administrator with userAcount of. userACalendar permissions from: userA-owner; userB-editor-delegate.

I'm not getting any errors and get Always displays the delegate indicator. I also deleted the user and added again, but the flag is still there.

I am the exchange administrator using an "admin" account (according to goDaddy). The host is not Microsoft but the o365 abomination of GoDaddy.

Am I doing something wrong?

console – PowerShell slow start over VPN

I have a problem with the startup speeds of PowerShell on which I performed an analysis.

When I start my laptop, PowerShell starts correctly (startup <1 s, including loading the profile of 1000 lines). When I start my corporate VPN (Check Point EndPoint), PowerShell then takes around 6.5 seconds to start (awful).

So I moved all of my $profile functions, etc. in a separate file and put it in C:Profile_Extras.ps1 to avoid going on the network in any way $Profile, but this is now only one line, pointing to my C:Profile_Extras.ps1 file).

PowerShell now takes 3.5 seconds to start. A big improvement, but still really bad for simply opening a console session (!) On a very fast / modern laptop.

cd $(Split-Path $Profile) leads me to:
Microsoft.PowerShell.CoreFileSystem::\ad.ing.netWPSNLPUD200024YA64UGHomeMy DocumentsWindowsPowerShell

When I leave the corporate VPN network, this data is still available (it uses some kind of caching software so that I can reference my network profile on H:). In this state, PowerShell takes <1 second to charge again, which is fine.

So my question is, how do I tell PowerShell to redirect where it searches for $ Profile? Is it a registry setting or something?

I also looked at a few other articles, but without help. Why is PowerShell slow on my machine?