Showing posts with label todo. Show all posts
Showing posts with label todo. Show all posts

Friday, April 5, 2013

Managing assemblies with Powershell

A simple way to access assemblies and see what's in them. Still a work in progress. I hope to add more tricks to this post soon.

Load in the assembly
$assembly = [Reflection.Assembly]::LoadFile($assemblyPath)
or
$assembly = [Reflection.Assembly]::Load($assemblyName)

Get details on a type defined in the assembly
$type = $assembly.DefinedTypes | Where-Object {$_.Name -eq $typeName}
$type.DeclaredConstructors | ForEach-Object {$_.ToString()}
$type.DeclaredMethods | ForEach-Object {$_.ToString()}

To load the assembly for use
Add-Type -Path $assemblyPath #if assembly in file
or
Add-Type -AssemblyName $assemblyName #if assembly in GAC
For some reason $assemblyName must be the full name contrary to the documentation in Add-Type

Get all assemblies in current AppDomain
[AppDomain]::CurrentDomain.GetAssemblies() | Select-Object FullName | Sort-Object FullName

References:
System.Reflection.Assembly class
Add-Type cmdlet
AppDomain class


Windows Azure storage through Powershell

This is just a first stab at manipulating Windows Azure Storage via Powershell.

Get Microsoft.WindowsAzure.Storage.dll using NuGet 
  1. Navigate to this by first going to the Windows Azure Downloads site.
  2. Select .NET. 
  3. Select Client libraries under Resources.
  4. Instructions on how to download Windows Azure Storage are found here. 
Here is the code I'm using to download files from blob storage

Param (
    [Parameter(Mandatory=$true)] [String] $StorageAccountName,
    [Parameter(Mandatory=$true)] [String] $AccessKey,
    [Parameter(Mandatory=$true)] [String] $BlobFilename,
    [Parameter(Mandatory=$true)] [String] $LocalFilename,
    [String] $StorageAccountEndpoint = "http://$storageAccountName.blob.core.windows.net/"
)

Add-Type -Path '.\Microsoft.WindowsAzure.Storage.dll'

$BlobFilename = $BlobFilename.Replace('%20', ' ').Trim('/')
$storageCredentials = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials($StorageAccountName, $AccessKey)
$blobClient = New-Object Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient($StorageAccountEndpoint, $storageCredentials)
$blob = $blobClient.GetBlobReferenceFromServer($BlobFilename)
$stream = New-Object System.IO.FileStream($LocalFilename, [System.IO.FileMode]::Create)
$blob.DownloadToStream($stream)
$stream.Close()





Wednesday, April 13, 2011

Parsing IIS logs with Powershell (because logparser not supported on Windows 2008)

I was very disappointed to find out that logparser is not supported on Windows 2008. So, I started to look for an alternative and found a TechNet post by Nick Goude on how to use Powershell to parse IIS logs.

I have, for the most part, simply lifted the code:


# Location of IIS LogFile
$File = "C:\inetpub\logs\LogFiles\W3SVC25824252\u_ex1104*.log"


# Get-Content gets the file, pipe to Where-Object and skip the first 3 lines.
$Log = Get-Content $File | where {$_ -notLike "#[D,S-V]*" }


# Replace unwanted text in the line containing the columns.
$Columns = (($Log[0].TrimEnd()) -replace "#Fields: ", "" -replace "-","" -replace "\(","" -replace "\)","").Split(" ")


# Count available Columns, used later
$Count = $Columns.Length


# Strip out the other rows that contain the header (happens on iisreset)
$Rows = $Log | where {$_ -notLike "#Fields"}


# Create an instance of a System.Data.DataTable
#Set-Variable -Name IISLog -Scope Global
$IISLog = New-Object System.Data.DataTable "IISLog"




# Loop through each Column, create a new column through Data.DataColumn and add it to the DataTable
foreach ($Column in $Columns) {
  $NewColumn = New-Object System.Data.DataColumn $Column, ([string])
  $IISLog.Columns.Add($NewColumn)
}


# Loop Through each Row and add the Rows.
foreach ($Row in $Rows) {
  $Row = $Row.Split(" ")
  $AddRow = $IISLog.newrow()
  for($i=0;$i -lt $Count; $i++) {
    $ColumnName = $Columns[$i]
    $AddRow.$ColumnName = $Row[$i]
  }
  $IISLog.Rows.Add($AddRow)
}


$IISLog

Now, if you save this to a file such as iislog.ps1, then you can run commands like:

.\iislog.ps1 | Select-Object csusername | Sort-Object -Property csusername | Get-Unique -AsString


Note, there are some glaring deficiencies:

  1. Parameterize the specification of log files
  2. Handle column name changes
  3. Handle extra headers (these are saved upon iisreset) - done
  4. Stream results back out so that they can be used in a pipeline

I hope to fix these soon, but need to get to sleep.

Thursday, January 13, 2011

Insufficient winsock resources available to complete socket connection initiation / tcp error code 10048

I have been trying to track down this problem for some time. Here are the symptoms I have seen.
There are a bunch of "TCP error code 10048: Only one usage of each socket address (protocol/network address/port) is normally permitted" messages in the Application Log
There are a bunch of "Insufficient winsock resources available to complete socket connection initiation"
I cannot connect to my SharePoint site or sometimes cannot even open central administration
Running netstat and TCPView results in thousands of connections in TIME_WAIT

I am running Windows 2008 x64 R2. According to KB929851 The default dynamic port range for TCP/IP has changed in Windows Vista and in Windows Server 2008, the number of ports has been bumped from a default range of 1025-5000 to a range of 49152-65535. So, bumping up the value to  HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\MaxUserPort does not help at all.

Also, these sockets are stuck in TIME_WAIT. By default the TIME_WAIT delay HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TCPTimeWaitDelay is 240 so these should have been cleaned up after 4 minutes anyway. So, why are they not being cleaned up?

Anyone who has an idea of what's going on, please feel free to comment. In the meantime, I'm just going to reboot.

Another reference: Hurry Up and TIME_WAIT

Tuesday, December 21, 2010

Random error with User Profile Service Application remedied by reboot

Some of my servers are running into an intermittent problem. Every so often, the User Profile Service Application is not accessible. A reboot seems to fix the issue, but I don't know why and haven't found anything conclusive out there. Here's what I am seeing in the ULS logs:


User Profile Application Proxy failed to retrieve partitions from User Profile Application: Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException: No User Profile Application available to service the request. Contact your farm administrator.
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_ApplicationProperties()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_PartitionIDs()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.IsAvailable(SPServiceContext serviceContext) a99603f0-3dc1-4312-af7a-5e2240082814
User Profile Application Proxy failed to retrieve partitions from User Profile Application: Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException: No User Profile Application available to service the request. Contact your farm administrator.
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_ApplicationProperties()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_PartitionIDs()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.IsAvailable(SPServiceContext serviceContext) a99603f0-3dc1-4312-af7a-5e2240082814
User Profile Application Proxy failed to retrieve partitions from User Profile Application: Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException: No User Profile Application available to service the request. Contact your farm administrator.
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_ApplicationProperties()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_PartitionIDs()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.IsAvailable(SPServiceContext serviceContext) a99603f0-3dc1-4312-af7a-5e2240082814
User Profile Application Proxy failed to retrieve partitions from User Profile Application: Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException: No User Profile Application available to service the request. Contact your farm administrator.
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_ApplicationProperties()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_PartitionIDs()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.IsAvailable(SPServiceContext serviceContext) a99603f0-3dc1-4312-af7a-5e2240082814
User Profile Application Proxy failed to retrieve partitions from User Profile Application: Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException: No User Profile Application available to service the request. Contact your farm administrator.
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_ApplicationProperties()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_PartitionIDs()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.IsAvailable(SPServiceContext serviceContext) a99603f0-3dc1-4312-af7a-5e2240082814






Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException: No User Profile Application available to service the request. Contact your farm administrator.    at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.get_ApplicationProperties()
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.CheckAdministrationAccess(UserProfileApplicationAdminRights rights, Boolean requireAllRights)
     at Microsoft.Office.Server.Administration.UserProfileApplicationProxy.CheckAdministrationAccess(UserProfileApplicationAdminRights rights)
     at Microsoft.Office.Server.UserProfiles.ProfileManagerBase.CanManagePeople(UserProfileApplicationProxy userProfileApplicationProxy)
     at Microsoft.Office.Server.UserProfiles.ProfileManagerBase.get_IsProfileAdmin()
     at Microsoft.Office.Server.UserProfiles.UserProfileManager..ctor(SPServiceContext serviceContext, Boolean IgnoreUserPrivacy, Boolean backwardCompatible)
     at Microsoft.Office.Server.UserProfiles.UserProfileManager..ctor(SPServiceContext serviceContext, Boolean IgnoreUserPrivacy)
     at NewsGator.Social.Library.Users.CachingUserProfileManager.<>c__DisplayClass10.<GetCachedInstance>b__f()
     at NewsGator.Social.Library.Caching.NGCache.RetrieveAndCache[T](String cacheKey, TimeSpan ts, Func`1 dataLoader)
     at NewsGator.Social.Library.Users.CachingUserProfileManager.GetCachedInstance(Boolean ignorePrivacy)
     at NewsGator.Social.Library.UserManager..ctor()
     at NewsGator.Social.Web.ClientRuntime.RegisterPageScripts(Page page, ScriptManager scriptManager)
     at NewsGator.Social.Web.CommunitiesOverviewWebPart.CreateChildControls()
     at System.Web.UI.Control.EnsureChildControls()
     at System.Web.UI.Control.PreRenderRecursiveInternal()
     at System.Web.UI.Control.PreRenderRecursiveInternal()
     at System.Web.UI.Control.PreRenderRecursiveInternal()
     at System.Web.UI.Control.PreRenderRecursiveInternal()
     at System.Web.UI.Control.PreRenderRecursiveInternal()
     at System.Web.UI.Control.PreRenderRecursiveInternal()
     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) a99603f0-3dc1-4312-af7a-5e2240082814






Exception occured while connecting to WCF endpoint: System.ServiceModel.CommunicationException: Could not connect to http://[hostname removed]:32843/06721706d40049bf9fe12b94be8f1912/ProfilePropertyService.svc. TCP error code 10048: Only one usage of each socket address (protocol/network address/port) is normally permitted [ip address removed]:32843.
  ---> System.Net.WebException: Unable to connect to the remote server
 ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted [ip address removed]:32843
     at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
     at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
     --- End of inner exception stack trace ---
     at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
     at System.Net.HttpWebRequest.GetRequestStream()
     at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream()
     --- End of inner exception stack trace ---
    Server stack trace: 
     at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream()
     at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
     at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)
     at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
     at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)
     at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
     at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
     at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)    Exception rethrown at [0]: 
     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
     at System.Runtime.Remoting.Proxies.RealProxyPrivateInvoke(MessageData& msgData, Int32 type)
     at Microsoft.Office.Server.UserProfiles.IProfilePropertyService.GetProfileProperties()
     at Microsoft.Office.Server.UserProfiles.ProfilePropertyServiceClient.<>c__DisplayClass1.<GetProfileProperties>b__0(IProfilePropertyService channel)
     at Microsoft.Office.Server.UserProfiles.MossClientBase`1.ExecuteOnChannel(String operationName, CodeBlock codeBlock)
     at Microsoft.Office.Server.UserProfiles.ProfilePropertyServiceClient.ExecuteOnChannel(String operationName, CodeBlock codeBlock) 

Thursday, December 9, 2010

Access denied errors from crawl

My crawl log was showing a couple of top level errors with this message:
Access is denied. Verify that either the Default Content Access Account has access to this repository, or add a crawl rule to crawl this repository. If the repository being crawled is a SharePoint repository, verify that the account you are using has "Full Read" permissions on the SharePoint Web Application being crawled

It turns out that I should have disabled the loopback check as mentioned in SharePoint disable loopback check (DisableLoopbackCheck in registry). This is just a matter of setting a DWORD value for DisableLoopbackCheck in the registry under HKLM\SYSTEM\CurrentControlSet\Contro\Lsa

Curious, I did some more research on what DisableLoopbackCheck actually does and came across KB896861 which mentions that the preferred approach would be to create a MultiString Value BackConnectionHostNames in the registry under HKLM\SYSTEM\CurrentControlSet\Contro\Lsa\MSV1_0 and add a list of your host names.

I also found this reference about DisableLoopbackCheck & SharePoint: What every admin and developer should know. This explains why the latter is better for security reasons.

Unfortunately, when I tried the latter, the http start address crawled properly but the sps3 one would not. Since I was short on time and this was just a demo server I decided to cheat and go with the former. Maybe someday I will reinvestigate this.

Monday, December 6, 2010

SharePoint KB983497, KB2281364, FIMSynchronizationService, User Profile Service Application synchronization issues

So, I needed to install hotfix KB983497 on my SharePoint 2010 server today because I needed a particular bug fixed. This was pretty straightforward. However, it breaks various things with user profile. For example, if you go to Manage User Profiles, you get a "File Not Found". You will also see something like this in the ULS log:
Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.ResourceManagement, Version=4.0.2450.9, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. File name: 'Microsoft.ResourceManagement, Version=4.0.2450.9, Culture=neutral, PublicKeyToken=31bf3856ad364e35' at Microsoft.Office.Server.UserProfiles.UserProfileConfigManager.InitializeIlmClient(String ILMMachineName, Int32 FIMWebClientTimeOut) at Microsoft.Office.Server.UserProfiles.UserProfileConfigManager..ctor(UserProfileApplicationProxy userProfileApplicationProxy, Guid partitionID) at Microsoft.SharePoint.Portal.WebControls.UserProfileServiceStatisticsWebPartBase.Loa...

The good thing is that the KB clearly states that you also need to install hotfix KB2281364 to fix this problem. This was also quite straightforward.

So, I go into the User Profile Service Application to create a synchronization connection, but I get this:
originalMaConfiguration.Create or UpdateResource failed at step Create Connection --- Microsoft.ResourceManagement.WebServices.Faults.ServiceFaultException: Unable to process Create message at Microsoft.ResourceManagement.WebServices.Client.ResourceTemplate.CreateResource() at Microsoft.ResourceManagement.WebServices.ResourceManager.CreateResource() at Microsoft.Office.Server.UserProfiles.DirectoryServiceConnection.UpdateInternal()

and I noticed that the Forefront Identity Management Service no longer starts. There are a bunch events similar to this one:
Microsoft.ResourceManagement.Workflow.Hosting.WorkflowManagerException: Forefront Identity Management Service does not support workflows of type 'Microsoft.ResourceManagement.Workflow.Activities.SequentialWorkflow, Microsoft.ResourceManagement, Version=4.0.2450.9, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
So, apparently, this one does not affect the profile synchronization feature, so you can ignore it. More about this at KB2432041

But also, it complains about a bunch of things including the following in the Application Log:
The Forefront Identity Manager Service cannot connect to the SQL Database Server.

The SQL Server could not be contacted. The connection failure may be due to a network failure, firewall configuration error, or other connection issue. Additionally, the SQL Server connection information could be configured incorrectly.

Verify that the SQL Server is reachable from the Forefront Identity Manager Service computer. Ensure that SQL Server is running, that the network connection is active, and that the firewall is configured properly. Last, verify the connection information has been configured properly. This configuration is stored in the Windows Registry.
To resolve this, I followed the suggestion at FIMSynchronizationService and Sharepoint 2010 August update which goes like this:
  1. Go to Central Administration->System Settings->Manage Services on Server
  2. Stop the User Profile Service
  3. Stop the User Profile Synchronization Service
  4. Start the User Profile Service
  5. Start the User Profile Synchronization Service and wait for it to finish starting
This will cause the 2 FIM Services to be started.

However, now I get the following:
UserProfileServiceUserStatisticsWebPart:LoadControl failed, Exception: System.IO.FileLoadException: The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) at Microsoft.Office.Server.UserProfiles.UserProfileConfigManager.InitializeIlmClient(String ILMMachineName, Int32 FIMWebClientTimeOut) at Microsoft.Office.Server.UserProfiles.UserProfileConfigManager..ctor(UserProfileApplicationProxy userProfileApplicationProxy, Guid partitionID) at Microsoft.SharePoint.Portal.WebControls.UserProfileServiceStatisticsWebPartBase.LoadControl(Object sender, EventArgs e)

To fix this, I found a post on UserProfileServiceUserStatisticsWebPart:LoadControl failed. To resolve, just do the following:
  1. Verify that the Forefront Identity Management Synchronization Service and the Forefront Identity Manager Service are running. This should have been started by the steps above, but you can verify it anyway by going to Start->Administrative Tools->Services
  2. Run iisreset, since Central Administration and User Profile Synchronization Service are provisioned on the same server. (I'm not sure why, though)
  3. Now go into User Profile Service Application and continue as normal.