Get All Datbases in your SQL Server

Posted on Updated on

This SQL script is used to get all databases in your SQL Server with details as follow (Thanks to Tibor Karaszi):

  1. Database name
  2. Data allocated
  3. Dat used
  4. Log allocated
  5. Log used
  6. recovery model
  7. Instance name

To get please do the following:

1. Run the below in a new SQL query

USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_dbinfo] Script Date: 07/28/2017 12:24:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROC [dbo].[sp_dbinfo]
@sort char(1) = 'n'
,@include_instance_name char(1) = 'n'
AS
/***************************************************************************
@sort accept 4 values: 'n' (default), 'd', 'l' and 'r'.
@include_server_name accept 2 values, 'y' and 'n'.
It specifies the sort order (name, data allocated, log allocated, rollup only).Written by Tibor Karaszi 2009-12-29
Modified 2010-01-19, fixed data type for db name. Thanks csm!
Modified 2010-05-24, added support for offline databases. Thanks Per-Ivan N?und.
Modified 2011-07-21, SQL Server 11, use sysperfinfo instead of DBCC SQLPERF.
Modified 2011-09-23, master instead of MASTER, also qualified sysperfinfo.
Modified 2011-12-28, renamed to sp_dbinfo, added rollup option.
Modified 2013-02-19, added recovery model and option for instance name.
***************************************************************************/
SET NOCOUNT ON
DECLARE
@sql nvarchar(2000)
,@db_name sysname
,@recovery_model varchar(12)
,@crlf char(2)SET @crlf = CHAR(13) + CHAR(10)--Create tables to hold space usage stats from commands
CREATE TABLE #logspace
(
database_name sysname NOT NULL
,log_size real NOT NULL
,log_percentage_used real NOT NULL
)
CREATE TABLE #dbcc_showfilestats
(
database_name sysname NULL
,file_id_ int NOT NULL
,file_group int NOT NULL
,total_extents bigint NOT NULL
,used_extents bigint NOT NULL
,name_ sysname NOT NULL
,file_name_ nvarchar(3000) NOT NULL
)
--Create table to hold final output
CREATE TABLE #final_output
(
database_name sysname
,data_allocated int
,data_used int
,log_allocated int
,log_used int
,is_sum bit
)
--Populate log space usage
INSERT INTO #logspace(database_name, log_size, log_percentage_used)
SELECT
instance_name AS 'Database Name'
,MAX(CASE
WHEN counter_name = 'Log File(s) Size (KB)' THEN cntr_value / 1024.
ELSE 0
END) AS 'Log Size (MB)'
,MAX(CASE
WHEN counter_name = 'Percent Log Used' THEN cntr_value
ELSE 0
END) AS 'Log Space Used (%)'
FROM master..sysperfinfo
WHERE counter_name IN('Log File(s) Size (KB)', 'Percent Log Used')
AND instance_name != '_total'
GROUP BY instance_name
----Populate data space usage
DECLARE db CURSOR FOR SELECT name FROM sys.databases WHERE state_desc = 'ONLINE'
OPEN db
WHILE 1 = 1
BEGIN
FETCH NEXT FROM db INTO @db_name
IF @@FETCH_STATUS 0
BREAK
SET @sql = 'USE ' + QUOTENAME(@db_name) + ' DBCC SHOWFILESTATS WITH NO_INFOMSGS'
INSERT INTO #dbcc_showfilestats(file_id_, file_group, total_extents, used_extents, name_, file_name_)
EXEC (@sql)
UPDATE #dbcc_showfilestats SET database_name = @db_name WHERE database_name IS NULL
END
CLOSE db
DEALLOCATE db
--Result into final table
INSERT INTO #final_output(database_name, data_allocated, data_used, log_allocated, log_used, is_sum)
SELECT
CASE WHEN d.database_name IS NOT NULL THEN d.database_name ELSE '[ALL]' END AS database_name
,ROUND(SUM(CAST((d.data_alloc * 64.00) / 1024 AS DECIMAL(18,2))), 0) AS data_allocated
,ROUND(SUM(CAST((d.data_used * 64.00) / 1024 AS DECIMAL(18,2))), 0) AS data_used
,ROUND(SUM(CAST(log_size AS numeric(18,2))), 0) AS log_allocated
,ROUND(SUM(CAST(log_percentage_used * 0.01 * log_size AS numeric(18,2))), 0) AS log_used
,GROUPING(d.database_name) AS is_sum
FROM
(
SELECT database_name, SUM(total_extents) AS data_alloc, SUM(used_extents) AS data_used
FROM #dbcc_showfilestats
GROUP BY database_name
) AS d
INNER JOIN #logspace AS l ON d.database_name = l.database_name
INNER JOIN sys.databases AS sd ON d.database_name = sd.name
GROUP BY d.database_name WITH ROLLUP
--Output result
SET @sql = '
SELECT f.database_name, f.data_allocated, f.data_used, f.log_allocated, f.log_used, d.recovery_model_desc' +
CASE @include_instance_name WHEN 'y' THEN ', @@SERVERNAME AS instance_name' ELSE '' END + @crlf +
'FROM #final_output AS f LEFT OUTER JOIN sys.databases AS d ON f.database_name = d.name' + @crlf +
CASE WHEN @sort = 'r' THEN 'WHERE f.database_name = ''[ALL]''' ELSE '' END + @crlf +
'ORDER BY is_sum' + @crlf +
CASE
WHEN @sort = 'n' THEN ', database_name'
WHEN @sort = 'd' THEN ', data_allocated DESC'
WHEN @sort = 'l' THEN ', log_allocated DESC'
ELSE ''
END
--PRINT @sql
EXEC(@sql)

2. This will create a Procedure with the name dbo.sp_dbinfo.
To confirmed please Check Databases > System Datbases > master > Programmabiltiy > Stored Procedures > dbo.sp_dbinfo

3. Now run this command

USE [master]
GO
EXEC sp_dbinfo 'd', 'y'

you should see result like the below

you can also pick one of those commands for different output

--Test execution
/*
EXEC sp_dbinfo
EXEC sp_dbinfo 'n'
EXEC sp_dbinfo 'd'
EXEC sp_dbinfo 'l'
EXEC sp_dbinfo 'r'
EXEC sp_dbinfo 'n', 'y'
EXEC sp_dbinfo 'd', 'y'
*/

Good Luck !

PowerShell for Show documents in Delve and in the Discover view in OneDrive for Business

Posted on Updated on

Delve is relatively still young baby for Microsoft.

Recently we have a requirement to enable the option of Show documents in Delve and in the Discover view in OneDrive for Business.

This can be done easily via a GUI like below

Go to App lancher and pick Delve –> Select Feature settings –> then enable Show documents in Delve and in the Discover view in OneDrive for Business

 

to do this via script, please copy and paste this in your PowerShell ISE and run it.

Pre-requistes

  1. Windows Internet Explorer
  2. You have to login at least 1 time through portal.office.com because the script will use your existing credentials


#Deactivate Delve Simulator
$ie = New-Object -COMObject InternetExplorer.Application
$jsonlink = "https://eur.delve.office.com/"
try{
#You can replace your the variable below with a static name, but her it logs with your current windows credentials
$searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
$mail = $searcher.FindOne().Properties.mail
$userName =$mail.replace("@","_").replace(".","_")
#Navigate to your download file/location
Write-host "Processing Request for: " $user.UPN
$ie.visible = $true
$ie.Navigate($jsonlink)
while($ie.Busy){Sleep 4}
while($ie.Document.location.href -like '*login.microsoftonline.com/*'){
(
$ie.document.IHTMLDocument3_getElementsByTagName("table") | Where-Object{$_.Id -eq $userName} | select -First 1).click()
Sleep 4
}
Write-Host "Ensure you are in the Delve Screem ..." -ForegroundColor Yellow
#[void](Read-Host 'Press Enter to continue after login ' -ErrorAction SilentlyContinue)
#Navigate to Properties
$ie.document.IHTMLDocument3_getElementById("O365_MainLink_Settings").click()
Sleep 4
#Open Settings
($ie.document.IHTMLDocument3_getElementsByTagName("a") | Where-Object{$_.className -eq "o365cs-settings-deeplink wf-size-x12 ms-fcl-ns o365button"} | select -First 1).click()
Sleep 4
##Switch Value
$checkBox =$ie.document.IHTMLDocument3_getElementById("enableDocumentsCheckbox")
$oldValue = $checkBox.value
if($oldValue -eq "on" -or $oldValue -eq "off")
{
$checked = $checkBox.click()
}
Sleep 2
#$newValue = $ie.document.IHTMLDocument3_getElementById("enableDocumentsCheckbox").value
##Submit
Sleep 1
($ie.document.IHTMLDocument3_getElementsByTagName("button") | Where-Object{$_.className -eq "SharingActivityPanel-module_button_delve"} | select -First 1).click()
##Refresh
Sleep 4
$ie.document.location = $ie.document.location
write-host "Done Successfully Delve Flag Switched"
}
catch [System.Exception]{
write-host "Failed, Try Again" $_.Exception
}
finally {
while($ie.Busy){Sleep 1}
$ie.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
}

How to Invoke Web Services from Microsoft System Center Orchestrator 2012?

Posted on

In this blog we are going to describe How to Call Web Service from System Center Orchestartor 2012, using the Invoke Webservices Activity

image

  1. We will elect a certain Webservice for test: http://www.ezzylearning.com/services/CountryInformationService.asmx
  2. Navigate to the URL to be sure it is working fine.
    Note : If you find this WebServices retired; please add your own or add a new one
    Example : http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
    All next steps should be the same
  3. Now we need to get the WSDL file from this webservice, so, either to click on the Service Description link or Navigate directly to
    http://www.ezzylearning.com/services/CountryInformationService.asmx?WSDL
    image
  4. Save this page to your hard drive
    image
  5. It is time now to call it from Orchestrator, In your runbook, Drag-Drop the Invoke Web Service Activity from the Utilities.
    image
  6. Set the properties of the Activity as follow:
    WSDL: Write the Path of the wsdl file that you have just save.
    Method: Select the required Web method, in this example we will use GetContients.
    Format Hint: it is Optional, sometimes it gives a result when you click it, depending on the web method it self, in our case, no Fomrat Hint is avaliable.image
  7. On the Advanced Tab, Check on the Address URL, and write the webservice full URL, http://www.ezzylearning.com/services/CountryInformationService.asmximage
  8. In the Security Tab, if the webservice have a HTTP Authentication, write it, other wise, leave it as it is, in our case, we will leave it as it is.
    image
  9. Add the activity of Append Line as shown, as we are going to display some output there
    image
  10. configure the Append line as shown.
    image
  11. Run your Runbook

You can parse the XML Response by Powershell script, or any other language to get your needed data.

Regards,

Cancel Nintex workflow stuck Instance using PowerShell

Posted on Updated on

In this post we are going to apply a PowerShell script to cancel all stucked workflow in Nintex

This script is used to run for SharePoint 2010, SQL Server 2008 R2, but it still can be used for other versions after running it on Stage environment

Take a copy from the below script and run it on the SharePoint server that contains Nintex workflows

if( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.SharePoint.PowerShell
}
$web = Get-SPWeb "https://Microsoft.com/Sites/YourSite";
$web.AllowUnsafeUpdates = $true;
$list = $web.Lists["Your WorkFlow Configuration list"];
foreach ($item in $list.Items)
{
foreach ($wf in $item.Workflows)
{
if ($wf.InternalState -eq "Completed")
{
# Nothing To Do
}
else
{
# Writes Workflow Status
Write-Host $wf.InternalState
#Cancel Workflow
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
}
}
}
$web.Dispose();

 

How to Change the Orchestrator Database ?

Posted on Updated on

You might have to change the location of the Orchestrator database after installation, because you might want to separate the management server and database server, move the database to a larger server or a cluster, or just reconfigure the orchestration database based on required changes in your environment. You can use standard Microsoft SQL Server methods to move the existing database to another server, but then you must configure the Orchestrator features to connect to the new server. You must perform this configuration for the management server, the web service supporting the Orchestration console, and each runbook server as described in the following procedures.

Management server and runbook servers

To change the database settings for the management server and runbook servers.

  1. On the management server, click Start, point to All Programs, click Microsoft System Center 2012, click Orchestrator, and then click Data Store Configuration.
  2. In the Server box, enter the name of the server that is hosting the database by using the format \,. You can click the ellipsis (…) button to select the computer. You do not have to include the instance if the Orchestrator database is installed on the default instance. You do not have to include the port if SQL Server is usually installed on the default port 1433.
    If the Orchestrator database is installed on an instance called MyInstance on a computer named MySQLServer that is configured on port 12345, enter MySQLServer\MyInstance,12345.
    If the Orchestrator database is installed on an instance called MyInstance on a computer named MySQLServer that is configured on port 1433, enter MySQLServer\MyInstance.
    If the orchestration database is installed on the default instance on a computer named MySQLServer that is configured on port 1433, enter MySQLServer.
  3. Select the authentication method to use to connect to the SQL Server:
    Windows Authentication Connect to the SQL Server by using Windows Authentication.
    SQL Server Authentication Connect to the SQL Server by using a SQL Server user account. Type the User Name and Password of the SQL Server user account. This account must have rights to create, write, and own a database and create, update, and delete rows in the database.
  4. Click Next.
  5. In the Data Store pane, click Use an existing database.
  6. In the Name list, select the databases.
  7. Click Finish.

Web Service

The web service supporting the Orchestration console does not use the Settings.dat file. To change the database settings for the web service, you must modify the Web.config file on the Internet Information Services (IIS) server. You can use IIS Manager to modify the file, but you must first decrypt it by running the aspnet_regiis.exe executable file.

To change the database settings for the Orchestrator web service

  1. Log on with administrative credentials to the computer with the Orchestration console installed.
  2. Open a Command Prompt window with administrator credentials.
  3. Run the following command to decrypt the Web.config file:
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pdf “connectionStrings” “C:\Program Files (x86)\Microsoft System Center 2012\Orchestrator\Web Service”
  4. To start the IIS Manager, click Start, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.
  5. Expand the Sites node, and then click Microsoft System Center 2012 Orchestrator Web Service.
  6. In the Features View, double-click Connection Strings.
  7. In the Connections String pane, double-click OrchestratorContext.
  8. In the Custom box, scroll down to the portion of the string that includes the server name (Data Source) and database name (Initial Catalog). Modify these values as required.
  9. Click OK to close the dialog box.
  10. Close IIS Manager.
  11. Run the following command to encrypt the Web.config file:
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pef “connectionStrings” “C:\Program Files (x86)\Microsoft System Center 2012\Orchestrator\Web Service”

Reference: Microsoft document for Administering Orchestrator in System Center 2010 Service Pack 1

Build Your First Orchestrator Runbook

Posted on Updated on

In this article we will intorduce how to build your first runbook using System Center Orchestrator 2012

  1. From the Start Menu, Select Runbook Designer

  2. If there is no New Runbook Area, Right-Click on the Runbook folder, and Select New Runbook

  3. On the New Runbook Area, We will drag and drop some Activities from the Activities List on the right pane.

  4. From the Scheduling tab, select Monitor Date/Time activity, and from Text File Management Tab, select the Append line activity, draw a link between the both activities.

  5. Now, we need to configure the properties of both activities, Right-Click on Monitor Date/Time Activity to set its Properties.

  6. We will set the Monitor Date/time to fire every 5 seconds
     
  7. For the Append Line Activity, Right-Click on it to set its properties.
  8. Set it as exists below, for the Text, we can add any static value, but to make the sample some more reasonable, We will add a dynamic variable, so, Right-Click on the Text box, and Select Subscribe > Published Data > Activity Start Time in UTC
  9. Now our runbook is ready for Run, Click Check In from the top bar.

  10. Note that the Run button is Activated, Click on it.
  11. Check the Out-put file, it should be something like this, Good Luck !