Monday, August 11, 2014

Find User SID using Powershell

These snippets allow to find a local or AD user SID or get the username from the SID
<#
.SYNOPSIS
Finds SID for a local user account.
.DESCRIPTION
Returns the SID of a local user account. To find the SID for an active directory user use
findADSID instead
.PARAMETER $user
The user for which to find the SID
.EXAMPLE
PS C:\> findLocalSID -user Guest
.INPUTS
System.String
.OUTPUTS
System.String
.LINK
about_functions_advanced
.LINK
about_comment_based_help
#>
function findLocalSID($user) {
$Objuser = New-Object System.Security.Principal.NTAccount($user)
$strSID = $Objuser.Translate([System.Security.Principal.SecurityIdentifier])
return $strSID.Value
}
<#
.SYNOPSIS
finds the SID for an Active directory user
.DESCRIPTION
returns a string in the form of an sid
.PARAMETER $domain
the domain the user belongs to
.PARAMETER $user
the domain user
.EXAMPLE
PS C:\> findADSID -domain <domain> -user <user>
.INPUTS
System.String
.OUTPUTS
System.String
.LINK
about_functions_advanced
.LINK
about_comment_based_help
#>
function findADSID{
param(
[Parameter(Mandatory = $true)]
$domain,
[Parameter(Mandatory = $true)]
$user)
$Objuser = New-Object System.Security.Principal.NTAccount($domain,$user)
$strSID = $Objuser.Translate([System.Security.Principal.SecurityIdentifier])
return $strSID.Value
}
<#
.SYNOPSIS
find User id based on SID Value
.DESCRIPTION
returns user name
.PARAMETER $sid
sid value (not quoted)
.EXAMPLE
PS C:\> findUserFromSID S-1-5-21-2213100911-2073097868-631715938-506
.INPUTS
System.String
.OUTPUTS
System.String
.NOTES
Additional information about the function goes here.
.LINK
about_functions_advanced
.LINK
about_comment_based_help
#>
function findUserFromSID{
param(
[Parameter(Mandatory = $true)]
$sid
)
$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
$Objuser = $objSID.Translate([system.Security.Principal.NTAccount])
return $Objuser.Value
}

Rename all files in directory using powershell part 2

Using Powershell 4 :

#Rename all files in a directory , setting all characters to lowercase and excluding directories
gci -Path D:\temp\ -File -Filter "txt*" | foreach{$_| Rename-Item -NewName $_.Name.ToLower()} -ErrorAction SilentlyContinue
#Rename all files in a directory , change only the first letter to lowercase
gci -Path D:\temp\ -File -Filter "*.txt" | foreach{$_| `
Rename-Item -NewName $($_.Name.substring(0,1).Tolower() + $_.Name.Substring(1))} -ErrorAction SilentlyContinue
#rename only x first file in directory , changing the first letter to lower case, if the source equals the destination we get an error message
gci -Path D:\temp\ -File -Filter "*.txt" | Select-Object -First 9 | foreach{$_| `
Rename-Item -NewName $($_.Name.substring(0,1).Tolower() + $_.Name.Substring(1))} -ErrorAction SilentlyContinue
#test if first letter is uppercase or lowercase to avoid errors
gci -Path D:\temp\ -File - Filter "*.txt"| Select-Object -First 10| foreach{$_| Rename-Item -NewName $(if($_.name.substring(0,1) -cmatch "[a-z]") {$_.Name.substring(0,1).ToUpper() + $_.Name.Substring(1)}
elseif ($_.Name.substring(0,1) -cmatch "[A-Z]") {$_.Name.substring(0,1).ToLower() + $_.Name.Substring(1)})
} -ErrorAction SilentlyContinue

Wednesday, August 6, 2014

Rename all files in a directory using Powershell

#All the files in the directory are uppercase i.e "TEST.TXT"
#We set the first letter to lowercase for this example
gci -Path D:\temp\ -File -Filter "*.txt" | foreach{$_| `
Rename-Item -NewName $($_.Name.substring(0,1).Tolower() + $_.Name.Substring(1))} -ErrorAction SilentlyContinue
 

Thursday, July 10, 2014

Open dtsconfig files in Notepad ++ (formatted as xml)


I often use Notepad++ as a quick way to open text files, and I want to open dtsconfig files formatted as xml :
in the settings Menu chose style configurator
image
then add the dtsconfig file extension to xml config:
image

Friday, May 30, 2014

SSRS 2008R2 refuses to start after upgrade from SSRS 2008

Scenario : After upgrading from SQL 2008 to SQL 2008 R2, reporting services refuses to start. All other SQL server services run normally after the upgrade. steps performed:

 If a AD Domain account is used, it has to be local admin for SSRS To start.
 Local services account do not work either.
uninstall/reinstall reporting services brought another issue : the setup finds - fails the IA64 OS check. The OS is not IA64.
Tried using the command line with the SKIPRULES switch without success.
New error Message : "SQL Path Element is missing" and the setup fails. 

I spent quit some time on Google looking for a solution for this, none of which works. I finally found out how to fix the issue:
 From the command line run
SETUP.EXE /ACTION=RunDiscovery.
On the report below we can notice 2 SQL2008 component for which the instance is highlighted : MSSQLSERVER.INACTIVE
Resolution Steps to remove the inactive instance:

 First search for files called Datastore_discovery.xml and open the most recent one. List of files left after uninstalling SQL Server Look for the places containing "MSSQLSERVER.INACTIVE" below is a sample
Urn="Machine[@ID='IWE']/Product[@ID='SQLVNEXT']/Instance[@ID='MSSQLSERVER.INACTIVE']" ID="MSSQLSERVER.INACTIVE" Name="MSSQLSERVER.INACTIVE" ........
Then look for the related lines containing ProductCode = "{B5153233-9AEE-4CD4-9D2C-4FAAC870DBE2}" (of course yours will be different)
The number of times you have to look for these product codes depend on how many components are listed as inactive. For each of the product codes that you found run: ** msiexec /x {B5153233-9AEE-4CD4-9D2C-4FAAC870DBE2} **
Do this for each product code related the MSSQLSERVER.INACTIVE that you found.

Then run setup again, click on Tools then "Installed SQL Server Features discovery report". If you can still see an instance MSSQLSERVER.INACTIVE you missed one of the Product Codes.

 After cleaning the server completely, I noticed : The existing SQL SERVER 2008R2 instance , SSIS etc.. were still running. I reinstalled SSRS and everything went fine. I did not perform any additional steps.

Wednesday, March 19, 2014

Configure SQL user to Write to the Event log

The case scenario is : I'm using a SSIS Script task to zip a large number of files and I want to log the 7zip errors to the Event log using System.Diagnostics. Here is the procedure I use to allow the SQL user to write to the event log.

You will need wevtutil normally found in %system32%, if not available you will have to use the registry editor.
1. configure credential for the user
2. determine permissions for the application log : wevtutil gl application > D:\temp\out.txt
3. open the file out.txt :
           name: application    
           enabled: true    
           type: Admin    
           owningPublisher:    
           isolation: Application    

            channelAccess: O:BAG:SYD:(A;;0xf0007;;;SY)(A;;0x7;;;BA)(A;;0x7;;;SO)(A;;0x3;;;IU)(A;;0x3;;;SU)(A;;0x3;;;S-1-5-3)(A;;0x3;;;S-1-5-33)(A;;0x1;;;S-1-5-32-573)
   
            logging:
  logFileName: %SystemRoot%\System32\Winevt\Logs\application.evtx
  retention: false
  autoBackup: false
  maxSize: 20971520
  publishing:
  fileMax: 1

4.          Using the Registry Editor (see original post  from Beth Massi)

You can allow any authenticated user write access to your log or you can allow the specific user SID write access. Open up the registry editor to your event log CustomSD key:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Eventlog\MyLogName\CustomSD

To allow all authenticated users write access add the following security permission:
(A;;0x0002;;;AU)
To allow a specific user write access you would specify their SID:
(A;;0x0002;;;SID-OF-USER-ACCOUNT) 
5.       Modify the registry using wevtutil 
 wevtutil sl Application /ca:<updated channel line>

 Example to give read/write access to authenticated users will will use the 0x3 value:
 wevtutil sl Application /ca: O:BAG:SYD:(A;;0xf0007;;;SY)(A;;0x7;;;BA)(A;;0x7;;;SO)(A;;0x3;;;IU)(A;;0x3;;;SU)(A;;0x3;;;S-1-5-3)(A;;0x3;;;S-1-5-33)(A;;0x1;;;S-1-5-32-573)(A;;0x3;;;AU)

Friday, February 21, 2014

Shrink TempDb Without Restart

USE tempdb
GO
--sp_helpfile
DBCC FREEPROCCACHE
GO
DBCC DROPCLEANBUFFERS
go
DBCC FREESYSTEMCACHE ('ALL')
GO
DBCC FREESESSIONCACHE
GO
--second parameter is targe in percent
DBCC SHRINKDATABASE(tempdb,10)

Monday, January 27, 2014

SSIS Parameter cannot be derived from sub-select queries


If you’re trying to use a parameter in a sub query using the SSIS SQL task you may encounter this error : image

Edit the SSIS Execute SQL Task and check the ByPassPrepare option
Currently it is set to false, and we generate the error above.


Set the Set the ByPassPrepare option to True:
  image
The task will now execute successfully !!